错误:错误状态:DocumentSnapshotPlatform 中不存在字段
Posted
技术标签:
【中文标题】错误:错误状态:DocumentSnapshotPlatform 中不存在字段【英文标题】:Error: Bad state : field does not exist within the DocumentSnapshotPlatform 【发布时间】:2021-09-28 01:41:32 【问题描述】:小部件库捕获的异常
在构建 FutureBuilder
相关的导致错误的小部件是:
FutureBuilder
代码:
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以上是关于错误:错误状态:DocumentSnapshotPlatform 中不存在字段的主要内容,如果未能解决你的问题,请参考以下文章
错误:错误状态:DocumentSnapshotPlatform 中不存在字段