【问题标题】:How to fetch all fields of a document in Firestore?如何在 Firestore 中获取文档的所有字段?
【发布时间】:2021-10-09 03:26:57
【问题描述】:

我使用此代码(在 Dart 中)从 Firebase Firestore 文档中获取所有字段并转换为对象列表:

final DocumentSnapshot documentSnapshot =
        await firestore.collection('configs').doc('stores').get();

    _allStores = documentSnapshot.data().values
        .map((store) => Store.fromMap(store))
        .toList();

但我目前正在使用 cloud_firestore:^2.4.0 和 documentSnapshot.data().values 中的 .values,不再受支持。如何在 Firestore 中获取文档的所有字段?

【问题讨论】:

    标签: firebase dart google-cloud-firestore


    【解决方案1】:

    据我所知,documentation 是一个 Map<String, dynamic>,其中所有值都直接来自 data()。所以:

    Map<String, dynamic> values = documentSnapshot.data() as Map<String, dynamic>;
    

    如果您必须指定类型,那(根据migrating to 2.0 上的文档)将是DocumentSnapshot&lt;Map&lt;String, dynamic&gt;&gt;

    【讨论】:

    • 如何将Map&lt;String, dynamic&gt; 转换为对象列表,就像之前的代码一样?
    • 据我所知,数据一直以Map&lt;String, dynamic&gt; 的形式公开,因为它是一组名称-值对。
    猜你喜欢
    • 2023-02-21
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多