0

i have tried to turn Future to nullable and also the string but something goes wrong any help please?

floatingActionButton: FloatingActionButton(
    onPressed: () {
      getName().then((value) {
        print(value);
        throw("error!!!!!");
      }).catchError((error) => print(error.toString()));
    },
    child: Icon(Icons.add),
),

out side the build method somewhere in my class

Future<String> getName() async =>'Some String';
nvoigt
  • 75,013
  • 26
  • 93
  • 142

2 Answers2

0

You can try this

getName().then((value) {
                print(value);
                throw ("error!!!!");
              }).catchError((error) {
                print("catch error :: " + error.toString());
              });

that code print catch error :: error!!!!. Is that you want?

Miftakhul Arzak
  • 1,166
  • 1
  • 12
  • 31
0

so this fixed my code

floatingActionButton: FloatingActionButton(
        onPressed: () => getName().then((value){
          //  the return type of an error handler is not acceptable
          // as the value of future.
          // after lambda expression is executed it gone so
          // we wouldn't know where the error came from
          // because also the return type is dynamic so we 
          //can't get to a datatype interface so it may be null since it's dynamic 
          if(value is FutureOr) {
            throw("error");
          }
        }).catchError((error) =>  print(error.toString()) ) ,