【问题标题】:http.dart invalid argument for onError: Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as argumentshttp.dart onError 参数无效:参数无效(onError):错误处理程序必须接受一个对象或一个对象和一个 StackTrace 作为参数
【发布时间】:2021-10-29 16:24:08
【问题描述】:

我是 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;
}

结果是:

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

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

【问题讨论】:

  • 你能发布一个最小的、可重现的例子吗?

标签: flutter http dart


【解决方案1】:

这是一个http 依赖错误,已在版本 0.12.2 中修复,更新依赖时应该会解决。

【讨论】:

  • 这确实导致了这个错误,并将 http 从 12.1 更新到 12.2 为我修复了它
【解决方案2】:

你也需要抓住ObjectHttpExceptionError 不涵盖所有类型。

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

【讨论】:

  • catch (e)on Object catch (e) 更惯用。问题中的错误也与未捕获的异常有关。
猜你喜欢
  • 1970-01-01
  • 2023-02-16
  • 2021-11-17
  • 1970-01-01
  • 2019-01-08
  • 1970-01-01
  • 2020-03-08
  • 2021-12-21
  • 1970-01-01
相关资源
最近更新 更多