Juc*_*oto 2 flutter google-cloud-firestore
我正在尝试从云 Firestore 记录制作一个简单的 GridView。我遵循了很多视频教程,但没有成功。这是代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class EventList extends StatefulWidget {
@override
EventListState createState() => new EventListState();
}
class EventListState extends State<EventList> {
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection('events_flutter').snapshots(),
builder: (BuildContext context, DocumentSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: const Text('Loading events...'));
}
return GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return Text(snapshot['event_name']);
},
itemCount: snapshot.data.documents.length,
);
},
);}}
Run Code Online (Sandbox Code Playgroud)
这是悬停在“构建器:(BuildContext 上下文,DocumentSnapshot 快照)”上时的错误消息。有人能帮我理解发生了什么吗?
非常感谢。
您应该替换snapshotfrom的类型DocumentSnapshot到AsyncSnapshot。
...
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(child: const Text('Loading events...'));
}
...
Run Code Online (Sandbox Code Playgroud)
而且,您可能想要替换此行:
return Text(snapshot['event_name']);
Run Code Online (Sandbox Code Playgroud)
对此:
return Text(snapshot.data.documents[index]['event_name']);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2509 次 |
| 最近记录: |