错误状态:DocumentSnapshotPlatform Flutter Firestore 中不存在字段
我遇到了问题标题中的错误,遵循官方颤振教程: https:/ /www.youtube.com/watch?v=DqJ_KjFzL9I
解决了 IDE 标记的所有错误,现在我的代码没有错误,但仅在运行应用程序时收到此消息。 我的 Firestore 数据库:
我的代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class Cloudtest extends StatefulWidget {
const Cloudtest({Key? key}) : super(key: key);
@override
_CloudtestState createState() => _CloudtestState();
}
class _CloudtestState extends State<Cloudtest> {
Widget _buildListItem(BuildContext context,DocumentSnapshot document){
return ListTile(
title: Text(document['name'])
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('todo').snapshots(),
builder: (context,AsyncSnapshot snapshot){
if (!snapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context,index) =>
_buildListItem(context,snapshot.data!.docs[index]),
);
}
),
);
}
}
我期望得到的是一个包含数据库中元素的列表,我用另一个 flutter 页面中的按钮调用此页面,应用程序的其余部分工作正常。 任何帮助表示赞赏!
I'm having troubles with the error in the question title, followed the official flutter tutorial: https://www.youtube.com/watch?v=DqJ_KjFzL9I
Solved all errors that the IDE marked, and now my code has no errors but only get this message when I run the app.
my firestore db:
My code:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class Cloudtest extends StatefulWidget {
const Cloudtest({Key? key}) : super(key: key);
@override
_CloudtestState createState() => _CloudtestState();
}
class _CloudtestState extends State<Cloudtest> {
Widget _buildListItem(BuildContext context,DocumentSnapshot document){
return ListTile(
title: Text(document['name'])
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('todo').snapshots(),
builder: (context,AsyncSnapshot snapshot){
if (!snapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context,index) =>
_buildListItem(context,snapshot.data!.docs[index]),
);
}
),
);
}
}
What I expected to get was a list with the elements in the database, I call this page with a button in another flutter page, the rest of the app works fine.
Any help aprecciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仍然需要从 DocumentSnapshot 中提取数据,如下所示:
此外,我在 Firebase 文档结构中没有看到 name 属性,您确实有 不过,标题和描述。
You still need to extract the data out of the DocumentSnapshot, as in:
Also I don't see the name property in the Firebase document structure, you do have title and description, though.