Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Instance of 'Model' error while trying collection query from Firebase

Hello I’am trying to query of a collection from Firebase.

In my service class, this is query function:

Future<List<ReviewModel>> getReviews(String salonUID) async {
    var _ref = FirebaseFirestore.instance
        .collection("users")
        .doc(userID)
        .collection('bucket')
        .doc('reviewbox')
        .collection('reviews');
    try {
      var _result = await _ref.get();
      List<ReviewModel> reviewList = List.empty(growable: true);

      for (var element in _result.docs) {
          print(element.data()); //print1
        reviewList.add(ReviewModel.fromJson(element.data()));
      }

      print(reviewList); //print2
      return reviewList;
    } on FirebaseException catch (e) {
      Get.snackbar("Error", e.code);
      rethrow;
    }
  }

print1 is returning me this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

...
{uid: uid, tarih: 24.3.2022 15:51, rate: 3, comment: comment2, userName: user name2, userID: userID2}
...

print2 is returning me this:

[Instance of 'ReviewModel', Instance of 'ReviewModel', Instance of 'ReviewModel']

Also, I already have 3 documents in Firebase.

How do I handle with print2 problem? I have to achive my model class.

EDIT

If I use this in FutureBuilder? How can I achive every field ?
I tried it but I didn’t make it.

 FutureBuilder<List<ReviewModel>>(
                  future:
                      FakeService().getSalonReviews(controller.salonModel.kUID),
                  builder: (context, snapshot) {
                    if (snapshot.hasData &&
                        snapshot.connectionState == ConnectionState.done) {
                      return ListView.builder(
                        shrinkWrap: true,
                        itemCount: snapshot.data!.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Text(snapshot.data![0].toString());
                        },
                      );
                    } else {
                      return Text("loading");
                    }
                  },
                )

enter image description here

>Solution :

Simply by reading whatever field you want of the objects. instead of

return Text(snapshot.data![0].toString());

do

return Text(snapshot.data![index].username);

or whatever field you want

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading