5

This is how my data look like. I'm trying to convert a map field object in Firestore to flutter.

enter image description here

If there isn't a rating map in my Firestore database, I can easily use the following code to convert the data in Flutter

firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
          .snapshots().listen((snapshot) {
        if (snapshot.docs != null) {
          var map = snapshot.docs;
          if (map != null) {
            map.forEach((change) {
              Review review = Review.fromJson(change.data());
            });
          }

But with the rating map in Firestore, I'm not sure how to convert it. Can anyone share how they would convert a map datatype in Firestore to flutter? Thanks in advance!

Below shows the code for the model.

class Reviews {
  String UserID;
  String UserName;
//what do I put for the rating

  Review({
    this.UserID,
    this.UserName
//what do I put for the rating
});

 factory ChatChannel.fromJson(Map<dynamic, dynamic> json) => ChatChannel(
      UserID: json['UserID'],
      UserName: json['UserName'],
//what do I put for the rating

 );

  Map<String, dynamic> toJson() => {
    "UserID": UserID,
    "UserName": UserName,
//what do I put for the rating

);
}

2 Answers 2

4

You can create a model for ratings and add its parameter in Review Model. Have a look in below code.

    class Reviews {
      String UserID;
      String UserName;
      Ratings ratings;
      Review({
        this.UserID,
        this.UserName
    //what do I put for the rating
    });
    
     factory ChatChannel.fromJson(Map<dynamic, dynamic> json) => ChatChannel(
          UserID: json['UserID'],
          UserName: json['UserName'],
          Ratings: Ratings.fromJson(json['ratings'])
     );
    
      Map<String, dynamic> toJson() => {
        "UserID": UserID,
        "UserName": UserName,
         "ratings": ratings.toJson(),
    
     );
   }

class Ratings{
  String oneStar;
  String twoStar;
  String threeStar;
  Ratings({this.oneStar, this.twoStar,this.threeStar});

factory Ratings.fromJson(Map<dynamic, dynamic> json) => Ratings(
   oneStar: json['1-star'],
   twoStar: json['2-star'],
   threeStar: json['3-star'],
  );

Map<String, dynamic> toJson() => {
   "1-star": oneStar,
   "2-star": twoStar,
   "3-star": threeStar,
);
}
Sign up to request clarification or add additional context in comments.

2 Comments

ok thank you :) let me try this and revert
thanks for your help. It worked! And thanks for your twitter clone. I got a lot of inspiration from that too :)
1

You can maintain the ratings field as a Map<String, int> type in the Reviews class like how you hold other fields.

class Reviews {
  Reviews({
    this.userID,
    this.userName,
    this.ratings,
  });

  String userID;
  String userName;
  Map<String, int> ratings;

  factory ChatChannel.fromJson(Map<dynamic, dynamic> json) => ChatChannel(
      userID: json['userID'],
      userName: json['userName'],
      ratings: json['ratings']);

  Map<String, dynamic> toJson() =>
      {"userID": userID, "userName": userName, 'ratings': ratings};
}

3 Comments

thanks, I tried your code but I received the following error: Unhandled Exception: type 'int' is not a subtype of type 'Map<String, int>'. do you know what's the issue?
Have you provided rating field type as Map<String, int>?
Hi Ashok, I managed to get it to work with TheAlphamerc's code. But thanks for your suggestion tho. Appreciate it! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.