首页 > 解决方案 > Flutter应用程序中的Json解析花费了太多时间

问题描述

我有一个包含大约 40,000 个条目的 json 文件。我试图在我的颤振应用程序中解析它。我正在获取 json 并将其成功转换为所需的模型。但问题是,加载时间过长。我的 json 文件存储在 Firebase 存储桶中。有没有其他方法可以快速加载?我正在考虑将数据存储在实时数据库中,但我相信它也会很快给出结果。任何建议都会很有帮助。

class CollegeCode {
  final String college_code;
  final String studentcode;
  final String college_Name;
  final String college_Location;

  CollegeCode({this.college_code, this.college_Name, this.college_Location, this.studentcode});

  factory CollegeCode.fromJson(Map<String, dynamic> json) =>
     CollegeCode(
      college_code: json['College Code'] ,
      college_Location: json['Location'],
      college_Name: json['College Name'],
      studentcode: json['Student Code']
    );

}
class GetList{
  GetList({this.data});
  List<CollegeCode> data;

  factory GetList.fromJson(Map<String, dynamic> json) {
    // print(json['CollegesList'].toString());
    if(json != null){

      return GetList(

        data: List<CollegeCode>.from(

            json["CollegesList"].map((x) => CollegeCode.fromJson(x))).toList() ?? [],
      );
    }
    return GetList(data: []);

  }
}

Future <List<CollegeCode>> parsePhotos(String responseBody) async{

    if(responseBody.isNotEmpty) {
      final parsed = await GetList.fromJson(json.decode(responseBody));

      final datalist = parsed.data.toList();
      return datalist;
    }

}



Future<List<CollegeCode>> fetchPhotos(http.Client client) async {
  final response =
  await client.get(url of file in storage bucket');

  // print(response.body);
  return parsePhotos(response.body);
  // return compute(parsePhotos, response.body);
}

我想知道是否有任何替代方法可以这样做。

标签: jsonflutter

解决方案


你确定是解码需要时间而不是通过网络传输这个巨大的文件吗?


推荐阅读