首页 > 解决方案 > 迁移 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'

标签: firebasedartfluttergoogle-cloud-firestore

解决方案


尝试使用 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);
                });
          }))

推荐阅读