简体   繁体   English

如何从 Firebase Firestore 集合中指定文档的特定字段获取数据并将其显示到我的 UI?

[英]How do I get data from a specific field of the specified document inside a Firebase Firestore collection and display it to my UI?

I tried the code below but in the String quantity = snapshot.data()['Quantity'];我尝试了下面的代码,但在字符串 quantity = snapshot.data()['Quantity']; 中function, it returns an error that says The method '[]' can't be unconditionally invoked because the receiver can be 'null'. function,它返回一个错误,指出无法无条件调用方法“[]”,因为接收者可以为“null”。 But when I tried to add a null check to it, it returns another error that says The operator '[]' isn't defined for the type 'Object'.但是当我尝试向它添加 null 检查时,它返回另一个错误,指出运算符 '[]' 没有为类型 'Object' 定义。 How do I make this function work?我如何使这个 function 工作? I'm trying to get data from the field 'Quantity' from the document typed in the Textfield I made and return a BottomSheet that shows the current quantity in the database.我正在尝试从我创建的BottomSheet Textfield

Future<void> _fabFunction(String dropdownValue, String quantityController) async{
    DocumentReference docRef = FirebaseFirestore.instance.collection('RAWWillyJKT').doc(_textfieldValue.text);

    docRef.get().then((DocumentSnapshot snapshot){
      if (snapshot.exists){
        Object? data = snapshot.data();
        String quantity = snapshot.data()['Quantity'];
        _scaffoldKey.currentState?.showBottomSheet((context) => Container(
          height: 300,
          child: Column(
            children: [
              Text('$quantity'),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text('Dismiss'))
            ],
          ),
        ));
        return snapshot.data();
      } else {
        FirebaseFirestore.instance
            .collection('RAWWillyJKT')
            .doc(_selectedOption)
            .set({
          'Jenis': _selectedOption,
          'Quantity': quantityController,
          'Metric': dropdownValue,
        });
      }
    });
  }

You declare data variable as Object?您将data变量声明为Object? and with this type there is no [] operator.对于这种类型,没有[]运算符。 The result of data() (if you don't use a converter) is Map<String, dynamic>? data()的结果(如果你不使用转换器)是Map<String, dynamic>? which does have the [] operator.它确实有[]运算符。

So if you already made sure that the data is not null, you can use:所以如果你已经确定数据不是null,你可以使用:

final data = snapshot.data()!;

After this you can access the fields:在此之后,您可以访问以下字段:

data['Quantity']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 StreamBuilder 显示我的 Cloud firestore 数据库中一个特定文档的数据? - How do I use StreamBuilder to display the data from ONE specific document in my Cloud firestore database? 如何使用 NextJs API、Firebase Firestore、axios 和 TypeScript 从 Firebase 集合中获取数据? - How do I get data from Firebase collection using NextJs API, Firebase Firestore, axios and TypeScript? 如何从 Firebase 的集合中的 Firestore 文档中获取单个字段? - How to fetch a single Field from a Firestore document in a collection from Firebase? 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website 无法从集合 firebase firestore 中获取所有文档 - Can't get all document from collection firebase firestore 如何使用模块 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合? - How do I get document and nested collection in a document in a Cloud Firestore collection using Version 9 of the Modular Web SDK? 如何将数据从我的数据库 (Firebase Firestore) 发送到我的 React Native 应用程序? - How do I send data from my database (Firebase Firestore) to my React Native app? 使用 ngxs-lab/firestore-plugin 时如何在我的 object 中获取 firebase 文档 ID - How to get the firebase document ID inside my object when using ngxs-lab/firestore-plugin 如何将 Firestore 文档引用存储为 nextjs 中的字段? - How do I store a Firestore document reference as a field from nextjs? 如何从 firebase 中的集合中的所有文档中获取特定数据? - How to get specific data from all documents from a collection in firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM