[英]Flutter snapshot.data() always null
I'm trying to display my Data from Firestore in my Flutter Web App, but I don't get any data.我试图在我的 Flutter Web 应用程序中显示来自 Firestore 的数据,但我没有获得任何数据。
Basically I just adjusted this example: https://firebase.flutter.dev/docs/firestore/usage#realtime-changes基本上我只是调整了这个例子: https://firebase.flutter.dev/docs/firestore/usage#realtime-changes
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class DataList extends StatefulWidget {
@override
_DataListState createState() => _DataListState();
}
class _DataListState extends State<DataList> {
@override
Widget build(BuildContext context) {
CollectionReference collectionReference = FirebaseFirestore.instance.collection('data');
return StreamBuilder<QuerySnapshot>(
stream: collectionReference.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
print('HasData: ${snapshot.hasData}');
if (snapshot.hasError) {
print(snapshot.error);
return Text('Error: ${snapshot.error}');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return new ListView(
children: snapshot.data.docs.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document.data()['name']),
subtitle: new Text(document.data()['description']),
);
}).toList(),
);
},
);
}
}
But snapshot.hasData is always null and I get this error: [cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges'
但 snapshot.hasData 始终为 null,我收到此错误: [cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges'
Getting a single Document works fine:获取单个文档工作正常:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class GetData extends StatelessWidget {
final String documentId;
GetData(this.documentId);
@override
Widget build(BuildContext context) {
CollectionReference collectionReference = FirebaseFirestore.instance.collection('data');
return FutureBuilder<DocumentSnapshot>(
future: collectionReference.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data.data();
return Text("Name: ${data['name']}, Description: ${data['description']}");
}
return Text("loading");
},
);
}
}
What am I doing wrong?我究竟做错了什么?
I don't really need it to be Realtime, btw.顺便说一句,我真的不需要它是实时的。
Try replacing your firestore script in index.html
file with this:尝试将index.html
文件中的 firestore 脚本替换为:
<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-firestore.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.