关于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