【问题标题】:Json parsing in flutter application taking too much timeFlutter应用程序中的Json解析花费了太多时间
【发布时间】:2021-01-07 18:16:36
【问题描述】:

我有一个包含大约 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);
}

我想知道是否有任何替代方法。

【问题讨论】:

    标签: json flutter


    【解决方案1】:

    不要期望立即处理 40 000 个条目。如果您希望使用本地数据库,sqflite package 非常适合此目的,因为它甚至提供批处理,可以在更短的时间内处理大量信息,尽管会消耗内存。

    此外,您可能需要检查您正在处理的信息量是否属于these parameters

    【讨论】:

    • 我不熟悉 sqflite 包。我一定会检查出来的。我想知道是否有任何替代品。这样我就不必一次解析这么多的条目了。
    【解决方案2】:

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

    【讨论】:

    • 我不确定。我正在使用未来的构建器来访问数据。我已经添加了 sn-p ,我正在尝试这样做。
    • 您的 sn-p 中没有危险信号。所以,我会在那里打印时间信息,以便准确查看哪个部分参与更多。像这样的东西:final stopwatch = Stopwatch()..start(); &lt;...expansive operation...&gt;; print('done in ${stopwatch.elapsed}');。因此,至少您可以弄清楚是服务器往返还是 json 解析杀死了您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2020-07-23
    • 2013-07-11
    • 2014-01-13
    • 1970-01-01
    相关资源
    最近更新 更多