flutter 优化之 dio jsondecode

关于Flutter优化,在最近项目中,讨论到网络请求jsoncode放到其它线程解析。同事提供了相关的解决方案,在此记录。

_parseAndDecode(String response) {
  return jsonDecode(response);
}

parseJson(String text) {
  return compute(_parseAndDecode, text);
}

final dio = Dio();
 (dio.transformer as DefaultTransformer).jsonDecodeCallback = parseJson;

参考

https://github.com/flutterchina/dio/blob/ac78e6151e1736f945cb9b215bbcfac230e19cf1/README.md
https://github.com/flutterchina/dio/issues/810
https://docs.flutter.dev/cookbook/networking/background-parsing

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容