Search code examples
flutterdartdart-null-safety

Unhandled Exception: type 'Null' is not a subtype of type 'Future<Never>'


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';

Solution

  • 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()) ) ,