http.dart invalid argument for onError: Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments

Viewed 3052

I am new to Flutter and I'd like to understand what's wrong with my error handler:

import 'package:http/http.dart' as http;
try {
  var response = await client.get('$endpoint/users/$userId');

  // Convert and return
  return User.fromJson(json.decode(response.body));
} on HttpException catch (ex) {
  print('http');
  print(ex);
  print('exception');
  return null;
} on Error catch (error) {
  // code will go here
  print(error);
  return null;
}

The result is:

I/flutter ( 3103): Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments.: Closure: (HttpException) => Null

It is exactly the same as if I didn't do any try/catch, could anyone advise how should I do the Error Handler? From my understanding (error) here should be the "one Object", please correct me if I'm wrong. TIA!

2 Answers

this is an http dependency bug and it was fixed in version 0.12.2, it should be resolved when you update your dependency.

You need to catch Object too. HttpException and Error do not cover all kinds.

    on Object catch (error) {
      print(error);
      return null;
    }
Related