【问题标题】:Load an array of key value pairs from Firestore into a List in flutter将一组键值对从 Firestore 加载到 Flutter 中的列表中
【发布时间】:2021-01-24 22:50:43
【问题描述】:

在将数据从 Firestore 映射到我在 Flutter 中的模型类方面需要一些帮助。

我的数据库结构:

下面是我的模型类,

class Exercises {

  String difficult, image, time, title;
  List<dynamic> MainExercise = [];

  Exercises.fromMap(Map<String, dynamic> data){

    title = data['title'];
    time = data['time'];
    difficult = data['difficult'];
    image = data['image'];
    MainExercise = List<dynamic>.from(data['MainExercise']);

  }


}

抛出错误,

[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The getter 'iterator' was called on null.
E/flutter (22059): Receiver: null
E/flutter (22059): Tried calling: iterator
E/flutter (22059): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)

我想从 Firestore 中读取 MainExercise array(以及其他字符串字段)并将其映射到我的练习 model 类,我将使用它来填充我的列表视图。

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    @sameer 给出的代码对我不起作用。我收到以下 3 个错误:

    • 错误:没有为“消息”类型定义方法“fromJson”。在以下行:

      ?.map( (e) => e == 空? null : MainExercise.fromJson(e as Map)) ?.toList();

    • 错误:构造函数不能返回值。在以下行:

    返回练习(

    • 错误:没有为类型“ListMessages”定义方法“fromMap”。在以下行: 消息:ListMessages.fromMap(mainExercise),

    我宁愿为此发表评论,但遗憾的是我的声誉还不够高。我是使用 Flutter 的新手,有些错误在现实中可能微不足道,但我可能仍然看不到。

    【讨论】:

    • 有点接近你的第一个 SO 评论。
    【解决方案2】:

    只要您的数据库中有一个嵌套的 Json 对象,就为它创建另一个类,然后在第一个类中使用它。

    您的类也不完整,没有构造函数或从 jsonMap 构造函数返回任何内容。

    由于MainExercise 字段是List&lt;Map&lt;String,dynamic&gt;&gt;,请执行此操作,

    class Exercises {
    
      String difficult; 
      String image;
      String time; 
      String title;
      List<MainExercise> mainExercise;
     
      Exercises({this.image, this.title, this.time, this.difficult, this.mainExercise});
    
      Exercises.fromMap(Map<String, dynamic> data){
    
        final String title = data['title'];
        final String time = data['time'];
        final String difficult = data['difficult'];
        final String image = data['image'];
        final List<Map<String,dynamic>> mainExercise = data['mainExercise'];
         
        return Exercises(
        image : image, 
        title : title,
        time : time , 
        difficult : difficult, 
        mainExercise : ListMainExercise.fromMap(mainExercise);
      }
    
    }
    
    class MainExercise {
      String exImage;
      String exName;
      String exTime;
    
    MainExercise({this.exImage, this.exName, this.exTime});
    
    factory MainExercise.fromMap(Map<String, dynamic> data){
       
       final exName = data['exName'];
       final exImage = data['exImage'];
       final exTime = data['exTime'];
       
      return MainExercise(
        exImage: exImage,
        exName: exName,
        exTime: exTime
       );
      }
    }
    
    
    class ListMainExercise{
    List<MainExercise> listExercise;
    
    ListMainExercise({this.listExercise});
    
       ListMainExercise.fromJson(Map<String,dynamic> data){
        (data as List)
            ?.map(
                (e) => e == null ? null : MainExercise.fromJson(e as Map<String, dynamic>))
            ?.toList()
    
       }
    }
    

    另外,建议使用 dart 约定,例如 camelCasing 和 factory 构造函数来创建 fromMap。

    编辑:这应该可以解决它,并且还建议使用json_serializable 包,使整个过程变得更加容易。

    【讨论】:

    • 感谢 Sameer 的详细回答,我明白了你的意思,但是我仍然收到错误,参数类型 'MainExercise' 无法分配给参数类型 'List 这个错误我我正在进入练习模型类返回语句,返回练习(图像:图像,标题:标题,时间:时间,困难:困难,mainExercise:MainExerciseModel.fromMap(mainExercise);'。
    • 哦,是的,很抱歉。我的错误,我将编辑我的答案。因为我们期待一个 List 我们必须以这种方式反序列化它。
    • @SouravGhosh,如果能解决您的问题,请接受我的回答。
    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 2022-11-12
    • 2020-08-06
    • 2018-11-21
    • 1970-01-01
    • 2021-05-24
    • 2019-11-21
    • 2022-11-23
    相关资源
    最近更新 更多