gpt4 book ai didi

Flutter snapshot.data() 始终为空

转载 作者:行者123 更新时间:2023-12-04 11:44:23 26 4
gpt4 key购买 nike

我试图在我的 Flutter Web 应用程序中显示来自 Firestore 的数据,但我没有得到任何数据。
基本上我只是调整了这个例子: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(),
);
},
);
}
}
但 snapshot.hasData 始终为空,我收到此错误: [cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges'获取单个文档工作正常:
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");
},
);
}
}
我究竟做错了什么?
我真的不需要它是实时的,顺便说一句。

最佳答案

尝试在 index.html 中替换您的 Firestore 脚本文件与此:

<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-firestore.js"></script>

关于Flutter snapshot.data() 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64843311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com