'Cloud Firestore query with Variable in Where clause is not working. Flutter

enter image description here class SetFirebaseDataSourceImpl implements SetFirebaseDataSource {

         final FirebaseFirestore firestore;
          SetFirebaseDataSourceImpl(this.firestore);
         @override
         Future<List<SetModel>> getAllSet(String subjectId) async {
             var data = await firestore
           .collection('set')
           .where('subjectId', isEqualTo: subjectId)
           .get();
            return data.docs.map((e) {
              var a = e.data();
              print('set $a');
              print(a);
               return SetModel.fromJson(a);
             }).toList();


          }
       }

it is not fetch document from firestore and when replace "mathId" in place of subjectId it workfine

when we remove variable (subjectId) and write actual value then it works fine Cloud Firestore query with Variable in Where clause isn't working. Flutter



Solution 1:[1]

you missed instance You wrote this

class SetFirebaseDataSourceImpl implements SetFirebaseDataSource {

     final FirebaseFirestore firestore;
      SetFirebaseDataSourceImpl(this.firestore);
     @override
     Future<List<SetModel>> getAllSet(String subjectId) async {
         var data = await firestore
       .collection('set')
       .where('subjectId', isEqualTo: subjectId)
       .get();
        return data.docs.map((e) {
          var a = e.data();
          print('set $a');
          print(a);
           return SetModel.fromJson(a);
         }).toList();


      }
   }

it is supposed to be this

class SetFirebaseDataSourceImpl implements SetFirebaseDataSource {

 final FirebaseFirestore firestore;
  SetFirebaseDataSourceImpl(this.firestore);
 @override
 Future<List<SetModel>> getAllSet(String subjectId) async {
     var data = await firestore.instance
   .collection('set')
   .where('subjectId', isEqualTo: subjectId)
   .get();
    return data.docs.map((e) {
      var a = e.data();
      print('set $a');
      print(a);
       return SetModel.fromJson(a);
     }).toList();


  }

}

Your code is okay.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1