错误:错误状态:DocumentSnapshotPlatform 中不存在字段

Posted

技术标签:

【中文标题】错误:错误状态:DocumentSnapshotPlatform 中不存在字段【英文标题】:Error: Bad state : field does not exist within the DocumentSnapshotPlatform 【发布时间】:2021-09-28 01:41:32 【问题描述】:

小部件库捕获的异常

在构建 FutureBuilder>(dirty, state: _FutureBuilderState>#b2b6c) 时引发了以下 StateError: 错误状态:DocumentSnapshotPlatform 中不存在字段

相关的导致错误的小部件是: FutureBuilder> file:///E:/​​Project/your_store/lib/tabs/home_tab.dart:15:11 抛出异常时,这是堆栈: #0 DocumentSnapshotPlatform.get._findKeyValueInMap (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:86:7) #1 DocumentSnapshotPlatform.get._findComponent(包:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:104:41) #2 DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:120:26) #3 _JsonDocumentSnapshot.get(包:cloud_firestore/src/document_snapshot.dart:92:48) #4 _JsonDocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:96:40)

代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:your_store/widgets/custom_action_bar.dart';
import 'package:your_store/widgets/product_cart.dart';

class HomeTab extends StatelessWidget 
  final CollectionReference _productRef =
      FirebaseFirestore.instance.collection("Products");

  @override
  Widget build(BuildContext context) 
    return Container(
      child: Stack(
        children: [
          FutureBuilder<QuerySnapshot>(
            future: _productRef.get(),
            builder: (context, snapshot) 
              if (snapshot.hasError) 
                return Scaffold(
                  body: Center(
                    child: Text("Error: $snapshot.error"),
                  ),
                );
              

              // Collection Data ready to display
              if (snapshot.connectionState == ConnectionState.done) 
                //Display data in List View
                return ListView(
                  padding: EdgeInsets.only(
                    top: 108.0,
                    bottom: 12.0,
                  ),
                  children: snapshot.data!.docs.map((document) 
                    return ProductCart(
                      title: document["name"],
                      imageUrl: document["images"[0]],
                      price: "\₹$document["price"]",
                      productId: document.id,
                    );
                  ).toList(),
                );
              

              // Loading State
              return Scaffold(
                body: Center(
                  child: CircularProgressIndicator(),
                ),
              );
            ,
          ),
          CustomActionBar(
            title: "Home",
            hasBackArrow: false,
          ),
        ],
      ),
    );
  

【问题讨论】:

【参考方案1】:

尝试使用“document.data()”,然后尝试通过访问键来获取值 例如:

children: snapshot.data!.docs.map((document) 
  // final Map<String, dynamic> data = document.data; // <== get the data of the document
  // Edit:
  final dynamic data = document.data();
  return ProductCart(
  title: data["name"],
  imageUrl: data["images"[0]],
  price: "\₹$data["price"]",
  productId: document.id,
 );
).toList(),

更新: 完整示例:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:your_store/widgets/custom_action_bar.dart';
import 'package:your_store/widgets/product_cart.dart';

class HomeTab extends StatelessWidget 
  final CollectionReference _productRef =
      FirebaseFirestore.instance.collection("Products");

  @override
  Widget build(BuildContext context) 
    return Container(
      child: Stack(
        children: [
          FutureBuilder<QuerySnapshot>(
            future: _productRef.get(),
            builder: (context, snapshot) 
              if (snapshot.hasError) 
                return Scaffold(
                  body: Center(
                    child: Text("Error: $snapshot.error"),
                  ),
                );
              

              // Collection Data ready to display
              if (snapshot.connectionState == ConnectionState.done) 
                //Display data in List View
                return ListView(
                  padding: EdgeInsets.only(
                    top: 108.0,
                    bottom: 12.0,
                  ),
                  children:
                      snapshot.data!.docs.map((QueryDocumentSnapshot document) 
                    // Map<String, dynamic> data = document.data();
                    final dynamic data = document.data();

                    return ProductCart(
                      title: data["name"],
                      imageUrl: data["images"[0]],
                      price: "₹$data["price"]",
                      productId: document.id,
                    );
                  ).toList(),
                );
              

              // Loading State
              return Scaffold(
                body: Center(
                  child: CircularProgressIndicator(),
                ),
              );
            ,
          ),
          CustomActionBar(
            title: "Home",
            hasBackArrow: false,
          ),
        ],
      ),
    );
  

【讨论】:

它给出了一个错误,即“对象”类型的值? Function()' 不能分配给“Map”类型的变量。 尝试将该行更改为:final dynamic data = document.data(); 局部变量'data'的值没有被使用。 用数据替换文档。但是记住要从document中获取id,所以把document.id保存在productId中。如果您还没有,请获取上面代码的副本。 其实我只是一个初学者,所以我对所有这些都不是很熟悉,所以如果你能通过在我的代码中实现这个给我一个例子,那将是一个很大的帮助。

以上是关于错误:错误状态:DocumentSnapshotPlatform 中不存在字段的主要内容,如果未能解决你的问题,请参考以下文章

错误:错误状态:DocumentSnapshotPlatform 中不存在字段

Python - Mysqldb 安装错误 [错误:命令错误退出状态为 1:]

传递状态时 Apollo Query 变量错误

BigQuery 插入错误,状态:挂起,状态码 5

错误:错误状态:流已被收听

嵌套两个流构建器导致错误状态错误