Home > Software design >  unhandled exception in flutter
unhandled exception in flutter

Time:08-16

the error has been caught but why these errors

onPressed: () {
            getName().then((value){
              print(value);
              print('The Last');
              throw('000');

            }).catchError((error){
              print('error (${error.toString()}) has been caught');
            });
        },

this is function

Future<String> getName() async
  {
    return 'Basel Elazaly';
  }
}

and these are output:- Unhandled Exception: Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the future's type

CodePudding user response:

onPressed: () async {
            await getName().then((value){ // without await it will return Future<String>
              print(value);
              print('The Last');
              throw('000');

            }).catchError((error){
              print('error (${error.toString()}) has been caught');
            });
        },

CodePudding user response:

Add a throw in catch error like this

onPressed: () {
            getName().then((value){
              print(value);
              print('The Last');
              throw('000');

            }).catchError((error){
              print('error (${error.toString()}) has been caught');
              throw("some other error");//<--here 
            });
        },
  • Related