【问题标题】:How to convert an array of map in Firestore to a List of map Dart如何将 Firestore 中的地图数组转换为地图 Dart 列表
【发布时间】:2020-05-27 10:17:26
【问题描述】:

我正在尝试使用 Flutter 和 Firestore 构建应用程序,但我对必须将数据从 Firestore 实现到 Dart 语言的方式感到困惑,所以问题是我的 Firestore 中有一个地图数组,数组中的每个元素都代表一个这样的地图

在我的应用程序中,我想将 Firestore 中的数据表示为可读的东西,我声明了一个这样的类:

class rest_model{
String rest_name;
GeoPoint rest_lat_long;
GeoPoint city_lat_long;
var rest_address = new Map();
var rest_food_list = new List<Map>();

我知道如何将其他转换为变量或可读,我使用这种方法:

rest_model.map(dynamic obj){

this.rest_name = obj['rest_name'];
this.rest_lat_long = obj['rest_lat_long'];
this.city_lat_long = obj['city_lat_long'];
this.rest_address['rest_email'] = obj['rest_address']['rest_email'];
this.rest_address['rest_phone']=obj['rest_address']['rest_phone'];
this.rest_address['rest_website'] = obj['rest_address']['rest_website'];
}

rest_model.fromMap(Map<String, dynamic> map){

this.rest_name = map['rest_name'];
this.rest_lat_long = map['rest_lat_long'];
this.city_lat_long = map['city_lat_long'];
this.rest_address['rest_email'] = map['rest_address']['rest_email'];
this.rest_address['rest_phone']=map['rest_address']['rest_phone'];
this.rest_address['rest_website'] = map['rest_address']['rest_website'];
}

Map<String, dynamic> toMap(){
var map = new Map<String, dynamic>();
map['rest_name'] = rest_name ;
map['rest_lat_long'] = rest_lat_long;
map['city_lat_long'] = city_lat_long;
map['rest_address'] = rest_address.values;
}

但是我不知道如何转换地图rest_food_list的数组。

请任何人都可以帮助我?

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:
    1. 在 dart 中创建一个类似于 firestore1 中的类。在 dart 中创建一个类似于 firestore 的类。我在 Dart 中有一个 Post 类,在 Firestore 中有一系列帖子。 Firestore Array of Map

    2. 我在 Dart 中的课程如下所示

    class Main{
      final String userName;
      final List<Post> post;
    
      Main({this.userName, this.post});
    }
    
    class Post {
      final String postImg;
      final int likes;
      final String description;
    
      Post({this.postImg, this.description, this.likes});
    }
    

    当我从 firestore 获取数据时,由于 firestore 有数组,所以你可以在 dart 中像这样转换 Map 的数组。

    //fetch firestore documents by getDocuments()
    data.documents.forEach((doc) {
          Main main = new Main(
              userImg: doc.data["user_image"],
              post: List<Post>.from(doc.data["posts"].map((item) {
                return new Post(
                    likes: item["likes"],
                    postImg: item["image"],
                    description: item["description"]);
              })));
          //add the main data object to List;
        }
    

    希望这会有所帮助!

    【讨论】:

    • 感谢您的帮助,它确实有效,但与字符串 food_name 一起工作,当我添加其他字段时,它不起作用,我试图重命名Firestore 集合中的字段,通过删除数组并使用字段名称的新名称创建新的字段,但我不知道为什么它不适用于另一个字段并且仅适用于一个字段 food_name 。 . 我真的不知道
    • 我是否应该使用另一个集合作为食物列表,并将餐厅的 id 作为新集合中的参考集合
    • 它也让我摆脱了障碍(即使是在 2020 年 ;-)),所以谢谢你!
    【解决方案2】:

    我以前没有这样做,但你可以试试这个:

    rest_model.fromMap(Map<String, dynamic> map) {
      this.rest_name = map['rest_name'];
      this.rest_lat_long = map['rest_lat_long'];
      this.city_lat_long = map['city_lat_long'];
      this.rest_address['rest_email'] = map['rest_address']['rest_email'];
      this.rest_address['rest_phone'] = map['rest_address']['rest_phone'];
      this.rest_address['rest_website'] = map['rest_address']['rest_website'];
      this.rest_food_list = map['rest_food_list'].map((Map<String, dynamic> restFood) {
        Map<String, dynamic> food = {
          'calories_plate': restFood['calories_plate'],
          'food_name': restFood['food_name'],
          'price': restFood['price'],
        };
        return food;
      }).toList();
    }
    

    【讨论】:

    • 不,我不是那个意思,我的意思是,我在 firestore 中有一个名为 restaurant 的集合,并且该集合中的每个文档都有这些字段( rest_name "String"、rest_lat_long "Geopoint" 和rest_food_list 是一个数组,数组中的每个 elemnet 都是一个 map ,并且 map 有三个 key,values .. 我知道如何转换其他字段但不知道如何转换 map 数组
    • 我的意思是将 firestore 中的地图数组转换为 dart 中的地图列表,反之亦然 @hoangquyy
    • @SunaArun 我已经编辑过了,你可以试试,不好意思我误会了
    • 没关系..谢谢你的帮助,但我没有解决..我该怎么办@hoangquyy
    • 但它只接受字符串类型,所以我将所有其他类型都更改为字符串..再次感谢
    猜你喜欢
    • 2013-05-25
    • 2019-12-05
    • 2020-07-03
    • 2021-02-06
    • 2022-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多