Kimetsu No Yaiba
Kimetsu No Yaiba

Reputation: 25

Load an array of key value pairs from Firestore into a List in flutter

Need some help regarding mapping data from Firestore to my model class in Flutter.

My database structure:

enter image description here

And my model class is below,

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 Thrown,

[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)

I want to read MainExercise array (along with other string fields) from Firestore and map it with my Exercises model class, which i will use to populate my list view.

Upvotes: 2

Views: 904

Answers (2)

The code given by @sameer does not work for me. I get the following 3 errors:

  • error: The method 'fromJson' isn't defined for the type 'Message'. On the following line:

    ?.map( (e) => e == null ? null : MainExercise.fromJson(e as Map<String, dynamic>)) ?.toList();

  • error: Constructors can't return values. On the following line:

return Exercises(

  • error: The method 'fromMap' isn't defined for the type 'ListMessages'. On the following line: messages : ListMessages.fromMap(mainExercise),

I would much rather leave a comment for this purpose but sadly my reputation isn’t high enough for that. I am new to using Flutter and some errors might be trivial in reality but I may still not see that.

Upvotes: 1

sameer kashyap
sameer kashyap

Reputation: 1166

Whenever you have a nested Json object in your database, create another class for it and then use it in the first class.

Also your classes are incomplete, does not have a constructor or return anything from the jsonMap constructor.

Since MainExercise field is a List<Map<String,dynamic>> do this,

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

   }
}

Also, recommend using dart conventions such as camelCasing and factory constructors to create fromMap.

Edit: That should fix it, and also recommend using json_serializable package, makes this entire process whole lot easier.

Upvotes: 2

Related Questions