Oscar Ogar
Oscar Ogar

Reputation: 13

Querying a specific value from a field that has more than one value in flutter from firestore

I am a complete newbie in programming altogether and this is my first question on this platform. I am writing a medical app in Flutter that works just like Uber using Firebase. I have two apps, (patients and health workers) and both share a single database. On the patient's app, there are icons representing all the medical workers. what I want to do is on the click of a specific icon, I want to fetch the specific 'user_type'(health workers specifically) and display the result on a ListView.

enter image description here

In the 'user-type' I have pharmacists, nurses, doctors, etc.

The code I have written is shown below.

The get function:

  void getDoctors() async {
    try {
      final doctors = await _firestore
          .collection('users')
          .where('user_type', isEqualTo: 'doctor')
          .getDocuments();
      for (var doctor in doctors.documents) {
        print(doctor);
      }
    } catch (e) {
      print(e);
    }
  }

Button code implemented in the scaffold:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: RaisedButton(
          child: Text(
            'search',
            style: kSendButtonTextStyle,
          ),
          onPressed: () {
            getDoctors();
          },
        ),
      ),
    );

The Output on the console is:

I/flutter ( 3262): Instance of 'DocumentSnapshot'

If it is not possible, should I change my database structure to focus on different users? For example, a different collection for each medical worker as opposed to them coming under one collection and identified only by the 'user_type" field in the Firestore.

Upvotes: 1

Views: 88

Answers (1)

user1506104
user1506104

Reputation: 7086

In your getDoctor() function, you need to change the following

for (var doctor in doctors.documents) {
    print(doctor);
}

to

for (var doctor in doctors.documents) {
    print(doctor.data);
}

Cheers!

Upvotes: 1

Related Questions