【发布时间】:2019-04-19 20:33:42
【问题描述】:
我正在将 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 How to bind a Firestore documents list to a Dropdown menu in Flutter? How to listen for document changes in Cloud Firestore using Flutter?
更新:
感谢大家的回复!我做了一些改变。
新代码:
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'
【问题讨论】:
-
错误是什么?
-
@UpaJah 对于“snapshot.data.documents.map(DocumentSnapshot 快照)”。错误:“无法命名函数表达式。”
标签: firebase dart flutter google-cloud-firestore