Search code examples
flutterfirebasegoogle-cloud-firestoreflutter-web

How to fix Error: Expected a value of type 'DatabaseUser', but got one of type '_JsonQuery'


I am trying to read from my firestore database. I am getting this error:

Error: Expected a value of type 'DatabaseUser$', but got one of type '_JsonQuery'

This is the method ran when trying to read the database:

Future ReadUserinDatabase(String email) async {
QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection('TEST').where('Email', isEqualTo: "[email protected]").get();
final allData1 = querySnapshot.docs.map((doc) => doc.get('Activity')).toList();
final allData2 = querySnapshot.docs.map((doc) => doc.get('CustomerID')).toList();
final allData3 = querySnapshot.docs.map((doc) => doc.get('Email')).toList();
final allData4 = querySnapshot.docs.map((doc) => doc.get('Role')).toList();
final allData5 = querySnapshot.docs.map((doc) => doc.get('UID')).toList();
print(allData1);
print(allData2);
print(allData3);
print(allData4);
print(allData5);
}
}

The DatabaseUser class:

class DatabaseUser {
  final String CustomerID;
  final String Email;
  final String Role;
  final String Activity;
  final String UID;

  DatabaseUser(
      {required this.Email,
      required this.CustomerID,
      required this.Role,
      required this.Activity,
      required this.UID});

  Map<String, dynamic> toJson() => {
        'CustomerID': CustomerID,
        'Email': Email,
        'Role': Role,
        'Activity': Activity,
        'UID': UID
      };

  static DatabaseUser fromJson(Map<String, dynamic> json) => DatabaseUser(
      Email: json['Email'],
      CustomerID: json['CustomerID'],
      Role: json['Role'],
      Activity: json['Activity'],
      UID: json['UID']);
}

This is the output I get when running the ReadUserinDatabase method

Read Method

This is my Firestore structure

Firestore Structure

I need to keep the user to pass the Role and UID so I can display specific things depending on their Role when they log in. Please Help!


Solution

  • I was using this way to extract specific items including what you mentioned in your application. I added a filter to search that Email address. I hope this way could resolve your question.

      void getMessagesTest222() async{
        QuerySnapshot querySnapshot = await _firestore.collection('Email').where('Email', isEqualTo: '[email protected]').get();
        final allData1 = querySnapshot.docs.map((doc) => doc.get('Activity')).toList();
        final allData2 = querySnapshot.docs.map((doc) => doc.get('CustomerID')).toList();
        final allData3 = querySnapshot.docs.map((doc) => doc.get('Email')).toList();
        final allData4 = querySnapshot.docs.map((doc) => doc.get('Role')).toList();
        final allData5 = querySnapshot.docs.map((doc) => doc.get('UID')).toList();
        print(allData1);
        print(allData2);
        print(allData3);
        print(allData4);
        print(allData5);
    
      }
    

    enter image description here enter image description here