http.dart onError 的无效参数:无效参数 (onError):错误处理程序必须接受一个对象或一个对象和一个 StackTrace 作为参数

Zen*_*aro 4 http dart flutter

我是 Flutter 新手,我想了解我的错误处理程序出了什么问题:

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;
}
Run Code Online (Sandbox Code Playgroud)

结果是:

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

这和我没有做任何事情是完全一样的try/catch,有人可以建议我应该如何做错误处理程序吗?根据我的理解,(error)这里应该是“一个对象”,如果我错了,请纠正我。蒂亚!

小智 6

这是一个http依赖项错误,已在版本 0.12.2 中修复,当您更新依赖项时应该可以解决该问题。