Home > Net >  Instance of 'Model' error while trying collection query from Firebase
Instance of 'Model' error while trying collection query from Firebase

Time:04-12

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:

...
{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

CodePudding user response:

Use inspect(reviewList) instead of print(reviewList).

CodePudding user response:

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

  • Related