迁移Flutter应用程序:实时数据库到Firestore

时间:2018-11-16 16:05:05

标签: firebase dart flutter google-cloud-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 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'

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

缺少括号:

children: snapshot.data.documents.map((DocumentSnapshot snapshot) {