簡體   English   中英

方法“數據”沒有為類型“對象”定義。 嘗試將名稱更正為現有方法的名稱,或定義名為“數據”的方法

[英]The method 'data' isn't defined for the type 'Object'. Try correcting the name to the name of an existing method, or defining a method named 'data'

我在顯示來自 Firebase 的數據時遇到問題。這是我在 FutureBuilder 中使用的代碼。 看起來有關於 data() 的錯誤,但我不知道是哪一個,有人知道嗎?

這就是我得到的:“未為類型‘對象’定義方法‘數據’。嘗試將名稱更正為現有方法的名稱,或定義名為‘數據’的方法。”

if(snapshot.connectionState == ConnectionState.done) {
  Map<String, dynamic> documentData = snapshot.data!.data() as Map<String, dynamic>;
     return ListView(
       children: [

         CustomSubtitle(
           text: "${documentData['01 - Brand']}"
         ),

         CustomTitle(
           text: "${documentData['02 - Name']}",
         ),

         CustomText(
          text: "${documentData['04 - Description']}",
         )

       ],
     );
   }

您正在使用QuerySnapshot的代碼。 對於這樣的代碼,您的代碼可以工作,但考慮到您如何使用您收到的結果數據DocumentSnapshot 為此,只需將您的代碼更改為:

if(snapshot.connectionState == ConnectionState.done) {
  Map<String, dynamic> documentData = snapshot.data as Map<String, dynamic>;
     return ListView(
       children: [

         CustomSubtitle(
           text: "${documentData['01 - Brand']}"
         ),

         CustomTitle(
           text: "${documentData['02 - Name']}",
         ),

         CustomText(
          text: "${documentData['04 - Description']}",
         )

       ],
     );
   }

如果有人是我的情況,我已經找到了解決方案:

builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {

    if(snapshot.connectionState == ConnectionState.done) {
    DocumentSnapshot<Object?> documentData = snapshot.data!;
     return ListView(
       children: [

           CustomSubtitle(
             text: "${documentData['01 - Brand']}"
           ),

           CustomTitle(
             text: "${documentData['02 - Name']}",
           ),

           CustomText(
             text: "${documentData['04 - Description']}",
           )

         ],
       );
     }
}
  future: collection.doc(documentId).get(),
  builder: ((context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      Map<String, dynamic> data =
          snapshot.data!.data() as Map<String, dynamic>;
      String currency = '${data['currency']}';
      
      return  Material(
          )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM