gpt4 book ai didi

android - flutter-如何加快解析Json函数的速度

转载 作者:行者123 更新时间:2023-12-03 03:40:00 26 4
gpt4 key购买 nike

我在bloc中有一个函数,需要从API检索数据,然后解析并将它们加载到小部件中。

对于从API进行数据检索,它是如此之快。但是,在解析json时,我看到了瓶颈,如下代码所示:

      response = await dio.get("/article/get-random?category=1&lang=${translations.currentLanguage}");
var data = response.data['data'];
print(data);
articleList.clear();
if (response.statusCode == 200) {

//data length is 5
for (int i = 0; i < data.length; i++) {

//Start of The Bottleneck is here...

model = await compute(articleFromJson, json.encode(data[i]));
articleList.add(model);

//End of The Bottleneck
}

print ("above code take around 5 seconds");

// sink to the stream
_articlesController.sink.add(articleList);
_loadingArticleController.sink.add(false);
await Storage.storeArticles(data);
}

此代码: model = await compute(articleFromJson, json.encode(data[i])); 大约需要3-5秒才能完成,有时需要更长的时间。

这是 articleFromJson的功能代码所在的类:
import 'dart:convert';

Article articleFromJson(String str) {
final jsonData = json.decode(str);
return Article.fromJson(jsonData);
}

String articleToJson(Article data) {
final dyn = data.toJson();
return json.encode(dyn);
}

class Article {

int id;
int category;
String name;
String baseUrl;
String url;
String imageUrl;
dynamic data;
int status;
int order;
DateTime createdAt;
DateTime updatedAt;
String categoryLabel;

Article({
this.id,
this.category,
this.name,
this.baseUrl,
this.url,
this.imageUrl,
this.data,
this.status,
this.order,
this.createdAt,
this.updatedAt,
this.categoryLabel,
});

factory Article.fromJson(Map<String, dynamic> json) => new Article(
id: json["id"],
category: json["category"],
name: json["name"],
baseUrl: json["base_url"],
url: json["url"],
imageUrl: json["image_url"],
data: json["data"],
status: json["status"],
order: json["order"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
categoryLabel: json["category_label"],
);

Map<String, dynamic> toJson() => {
"id": id,
"category": category,
"name": name,
"base_url": baseUrl,
"url": url,
"image_url": imageUrl,
"data": data,
"status": status,
"order": order,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
"category_label": categoryLabel,
};
}

Json Data本身如此简单,如下所示(仅5行):
{
"status": 200,
"message": "Success",
"data": [
{
"id": 990121,
"category": 1,
"name": "Article 1 Title",
"base_url": "base_url",
"url": "https://article_1",
"image_url": "https://ik.imagekit.io/xxx1",
"data": null,
"status": 1,
"order": 4,
"created_at": "2019-05-01 13:58:37",
"updated_at": "2019-07-24 15:25:23",
"category_label": "Article"
},
{
"id": 990122,
"category": 2,
"name": "Article 2 Title",
"base_url": "base_url",
"url": "https://article_2",
"image_url": "https://ik.imagekit.io/xxx2",
"data": null,
"status": 1,
"order": 4,
"created_at": "2019-05-01 13:58:37",
"updated_at": "2019-07-24 15:25:23",
"category_label": "Article"
},
{
"id": 990123,
"category": 1,
"name": "Article 3 Title",
"base_url": "base_url",
"url": "https://article_3",
"image_url": "https://ik.imagekit.io/xxx3",
"data": null,
"status": 1,
"order": 4,
"created_at": "2019-05-01 13:58:37",
"updated_at": "2019-07-24 15:25:23",
"category_label": "Article"
},
{
"id": 990124,
"category": 1,
"name": "Article 4 Title",
"base_url": "base_url",
"url": "https://article_4",
"image_url": "https://ik.imagekit.io/xxx4",
"data": null,
"status": 1,
"order": 4,
"created_at": "2019-05-01 13:58:37",
"updated_at": "2019-07-24 15:25:23",
"category_label": "Article"
},
{
"id": 990125,
"category": 1,
"name": "Article 5 Title",
"base_url": "base_url",
"url": "https://article_5",
"image_url": "https://ik.imagekit.io/xxx5",
"data": null,
"status": 1,
"order": 4,
"created_at": "2019-05-01 13:58:37",
"updated_at": "2019-07-24 15:25:23",
"category_label": "Article"
},
]
}

我的客户需要提高这种加载/解码速度,有什么想法吗?

最佳答案

您没有在应使用的compute()函数中使用compute()隔离函数中的JSON解析方法,请引用Parse JSON in the background

关于android - flutter-如何加快解析Json函数的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57551289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com