Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an array of map in Firestore to a List of map Dart

I am trying to build an app with Flutter and Firestore, but I am confused by the way I have to implement my data from Firestore to Dart language, so the problem is that I have an array of the map in my Firestore, each element in the array represent a map like this

screenshot

And in my app, I want to represent the data from Firestore to something readable, I declare a class like this:

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>();

I know how to convert the other into variables or readable, I use this approach:

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;
}

But I don't know how to convert the array of the map rest_food_list.

Please anyone can help me?

like image 588
Sana'a Al-ahdal Avatar asked Feb 11 '20 22:02

Sana'a Al-ahdal


1 Answers

  1. Create a class in dart similar to that in firestore1. Create a class in dart similar to that in firestore. I have a Post class in Dart and an array of posts in firestore. Firestore Array of Map

  2. My class in Dart is like below

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});
}

When I am fetching the data from firestore, since firestore has array so you can convert that array of Map like this in dart.

//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;
    }

Hope this helps!!

like image 173
Mohit Sharma Avatar answered Sep 19 '22 22:09

Mohit Sharma