firebase - 迁移 Flutter 应用程序:实时数据库到 Firestore
问题描述
我正在将 Flutter 应用程序从 Firebase 实时数据库迁移到 Firestore。我无法在聊天应用程序中更新此代码,因为 firestore 没有 FirebaseAnimatedList。
旧代码:
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(“chat“),
),
body: new Container(
child: new Column(
children: <Widget>[
new Flexible(
child: new FirebaseAnimatedList(
query: reference,
sort: (a, b) => b.key.compareTo(a.key),
padding: new EdgeInsets.all(8.0),
reverse: true,
itemBuilder: (_, DataSnapshot snapshot,
Animation<double> animation, int x) {
return new ChatMessage(
snapshot: snapshot, animation: animation);
},
),
),
新代码(但给我错误):
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(“chat"),
),
body: new Container(
child: new Column(
children: <Widget>[
new Flexible(
child: new StreamBuilder<QuerySnapshot>(
stream: reference.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
return snapshot.hasData? new ListView(
physics: const AlwaysScrollableScrollPhysics(),
reverse: true,
padding: new EdgeInsets.all(8.0),
children: snapshot.data.documents.map(DocumentSnapshot snapshot) {
return new ChatMessage(
snapshot: snapshot,
animation: animation,
);
})
),
参考:
final reference = Firestore.instance.collection('messages');
有什么帮助吗?
我查了一下: Firestore StreamBuilder with nested AnimatedList 如何将 Firestore 文档列表绑定到 Flutter 中的下拉菜单? 如何使用 Flutter 监听 Cloud Firestore 中的文档更改?
更新:
谢谢大家的回复!我做了一些改变。
新代码:
child: new StreamBuilder<QuerySnapshot>(
stream: reference.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot snapshot) {
return new ChatMessage(
snapshot: snapshot,
animation: animation,
);
}).toList(),
);
}
),
),
现在唯一的错误是动画。我有错误:undefined name 'animation'
解决方案
尝试使用 ListView.builder ..
new Flexible(
child: new StreamBuilder<QuerySnapshot>(
stream: reference.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
reverse: false,
shrinkWrap: true,
itemBuilder: (context, index) {
return ChatMessage(
animation, snapshot.data.documents[index], index);
});
}))
推荐阅读
- mongodb - Mongoose 在更新时附加到字符串
- javascript - 滑块切换后悬停效果不起作用
- sql - SQL Server - 如何将散列密码插入表?
- android-jetpack-compose - 如何解释 Android Jetpack Compose 中“组”的概念?
- python - 使用 RandomizedCV 调整随机森林分类器的问题
- python - (
, ProtocolError('无效数据或不支持的协议版本'), ) - html - Express js 处理来自动态创建的不同字段的多个文件上传
- python - 如何有效地绘制 torch.nn 模型图?
- node.js - 使用nodejs typescript将数据插入postgreSQL
- oracle-nosql - Oracle NoSQL - 如何在 MAP 中查找键以值开头的所有行