【问题标题】:Migrate Flutter app: realtime db to firestore迁移 Flutter 应用程序:实时数据库到 Firestore
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 感谢您的回复!我试过你的代码,但它给出了很多错误。我现在已经更新了我上面的代码。现在唯一的错误是在哪里定义“动画”。
【解决方案2】:

缺少一个括号:

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

【讨论】:

  • 谢谢!这有帮助,但我仍然有错误。我在上面的编辑中有更新。
  • 如果你需要动画,你需要在某处定义动画。先尝试让它在没有动画的情况下工作。
猜你喜欢
  • 2018-03-16
  • 2019-07-17
  • 1970-01-01
  • 2018-06-15
  • 2011-07-08
  • 2018-04-01
  • 2018-07-19
  • 1970-01-01
  • 2019-06-08
相关资源
最近更新 更多