简体   繁体   中英

displaying data from a specific docs within firestore collection

i need a way to display data from within a document instead of the entire collection, the snippet below displays entire data from all documennts within the collection, what method can i use to better achieve my request?

    final addMealRespositoryProvider = Provider((ref) => MealRespository());

class MealRespository {
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;
  final FirebaseStorage _storage = FirebaseStorage.instance;

  Future<void> writeAddMeal(Menu menu, {File? file}) async {
    final ref =
        _firestore.collection("menu").doc(menu.id.isEmpty ? null : menu.id);
    String? mealImageUrl = file != null
        ? (await (await _storage.ref("meal_images").child(ref.id).putFile(file))
            .ref
            .getDownloadURL())
        : null;

    await ref.set(
        menu.copyWith(mealUrl: mealImageUrl).toMap(), SetOptions(merge: true));
  }

  Stream<List<Menu>> get menuStream =>
      _firestore.collection("menu").snapshots().map(
            (event) => event.docs.map((e) => Menu.fromFirestore(e)).toList(),
          );

   
}

我的火库的照片

If you don't know the ID for the document, but know some other value that identifies it, you can use a query :

Stream<List<Menu>> get menuSearchStream =>
  _firestore.collection("menu").where("price", equalTo: "30").snapshots().map(
        (event) => event.docs.map((e) => Menu.fromFirestore(e)).toList(),
      );

If you know the ID of the document you want to display data for, you can create a stream on snapshots for that:

Stream<Menu> get singleMenuStream =>
  _firestore.collection("menu").doc("idForTheDocInQuestion").snapshots().map(
        (doc) => Menu.fromFirestore(doc)),
      );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM