Get a document in firbase based on the value of a parameter in the document

Viewed 17

Im following a turtorial on how to fetch data from firebase.

In this line, you get the document from the collection orders with the same id as user.id:

var ref = _db.collection('orders').doc(user.uid);

I want another document, namley not a document that has the same id as the user, but instead a document where the value userId is the same as user.uid, something like this:

var ref = _db.collection('orders').where('userId', isEqualTo: user.uid);

The problem is that the second line doesnt return same dataformat as the first and that gives the program an error which makes it impossible to directly replace the firs line whith the second, so I wonder if it is possible to return exactly the same dataformat as the first line but rather getting the document where the parameter userId is equal to user.uid, than the document id which I tried to do with the second line.

The full code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:test/services/auth.dart';
import 'package:test/services/models.dart';
import 'package:rxdart/rxdart.dart';

class FirestoreService {
  final FirebaseFirestore _db = FirebaseFirestore.instance;

  
  Stream<Order> streamReport() {
    return AuthService().userStream.switchMap((user) {
      if (user != null) {
        var ref = _db.collection('orders').doc(user.uid); <===================== The line i want to replace
        return ref.snapshots().map((doc) => Order.fromJson(doc.data()!));
      } else {
        return Stream.fromIterable([Order(startDate: DateTime.now())]);
      }
    });
  }
0 Answers
Related