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

1,735

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

Share:
1,735
Zennichimaro
Author by

Zennichimaro

Updated on December 31, 2022

Comments

  • Zennichimaro
    Zennichimaro over 1 year

    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!