【发布时间】:2020-03-12 15:51:51
【问题描述】:
我正在使用 Flutter Web 构建网站,但我无法将数据从 Firestore 获取到 StreamBuilder,因为 onSnapshot 不起作用...
我正在使用这个包https://pub.dev/packages/firebase
我的代码:
import 'package:flutter/material.dart';
import 'package:firebase/firebase.dart';
import 'package:firebase/firestore.dart' as fs;
class Playground extends StatefulWidget {
@override
_PlaygroundState createState() => _PlaygroundState();
}
class _PlaygroundState extends State<Playground> {
@override
Widget build(BuildContext context) {
fs.Firestore dataBase = firestore();
fs.CollectionReference teachersRef = dataBase.collection('teachers');
return Center(
child: Container(
width: 700,
height: 400,
color: Colors.cyanAccent,
child: StreamBuilder(
stream: teste.collection('teachers').onSnapshot,
builder: (context, snapshot) {
if (!snapshot.hasData) return Text('Loading...');
return ListView.builder(
//itemExtent: 30,
itemCount: 4,
itemBuilder: (context, index) {
return Container(
width: 10,
color: Colors.amber,
child: Text(snapshot.data[index]['name']),
);
},
);
},
),
),
);
} }
但我得到了这个答案:
'[]' Dynamic call of null. Receiver: Instance of 'QuerySnapshot' Arguments: [0]
有人知道如何在 Flutter web 上正确使用 Firestore 吗?
【问题讨论】:
-
希望您在
index.html中包含<script>标签。testevariable 定义在哪里。我没看到。还可以尝试为流构建器提供初始值。 -
嗨,你有没有让这个工作?每次我使用 onSnapshot 和 snapshot.hasData 时,我都没有从我的集合或文档请求中得到任何回报..
-
嗨!是的,我想出了如何使用...
-
要获取您需要的数据,请键入 snapshot.data.data()
标签: flutter google-cloud-firestore flutter-web