【问题标题】:FirebaseAnimatedList itemBuilder ErrorFirebaseAnimatedList itemBuilder 错误
【发布时间】:2018-08-10 03:51:10
【问题描述】:

我目前正在通过 Flutter Firebase 代码实验室here

我已完成所有步骤,但在尝试使用 FirebaseAnimatedList 小部件的 itemBuilder 时遇到两个错误。

错误 1

sort: (a, b) => b.key.compareTo(a.key)相关:

函数表达式类型“(动态,动态)→动态”不是类型“(DataSnapshot,DataSnapshot)→int”。这意味着它的参数或返回类型与预期不匹配。考虑更改参数类型或返回的类型。

我是否需要将(a,b) 转换为DataSnapshot

错误 2

FirebaseAnimatedListitemBuilder 相关。

参数类型'(BuildContext, DataSnapshot, Animation, int) → dynamic'不能分配给参数类型'(BuildContext, DataSnapshot, Animation) → Widget'。

在这种情况下,似乎需要传入索引,并且ChatMessage 正在返回错误的类型。我不确定如何解决这些问题。

下面是我的代码。

ChatScreenState 类的构建函数

 @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: Text('friendlychat'),
        ),
        body: Column(
          children: <Widget>[
            Flexible(
              child: FirebaseAnimatedList(
                query: reference,
                sort: (a, b) => b.key.compareTo(a.key),
                padding: EdgeInsets.all(8.0),
                reverse: true,
                itemBuilder: (BuildContext context, DataSnapshot snapshot,
                    Animation<double> animation, int index) {
                  return ChatMessage(snapshot: snapshot, animation: animation);
                },
              ),
            ),
            Divider(height: 1.0),
            Container(
              decoration: BoxDecoration(color: Theme.of(context).cardColor),
              child: _buildTextComposer(),
            )
          ],
        ));
  }

ChatMessage

class ChatMessage extends StatelessWidget {
  ChatMessage({this.snapshot, this.animation});
  final DataSnapshot snapshot;
  final Animation animation;

  @override
  Widget build(BuildContext context) {
    return SizeTransition(
        sizeFactor: CurvedAnimation(parent: animation, curve: Curves.easeOut),
        axisAlignment: 0.0,
        child: Container(
          margin: EdgeInsets.symmetric(vertical: 10.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(right: 16.0),
                child: CircleAvatar(
                    backgroundImage:
                        NetworkImage(snapshot.value['senderPhotoUrl'])),
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(snapshot.value['senderName'],
                        style: Theme.of(context).textTheme.subhead),
                    Container(
                      margin: EdgeInsets.only(top: 5.0),
                      child: Text(snapshot.value['text']),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ));
  }
}

更新了FirebaseAnimatedList的代码:

FirebaseAnimatedList(
                query: reference,
                sort: (DataSnapshot a, DataSnapshot b) =>
                    b.key.compareTo(a.key),
                padding: EdgeInsets.all(8.0),
                reverse: true,
                itemBuilder: (BuildContext context, DataSnapshot snapshot,
                    Animation<double> animation) {
                  return ChatMessage(snapshot: snapshot, animation: animation);
                },
              ),

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    这是因为 Flutter beta 使用的是 Dart-2,它默认确保最大类型安全。

    要解决错误 1: 改变

    (a, b) =&gt; b.key.compareTo(a.key)

    (Datasnapshot a, Datasnapshot b) =&gt; b.key.compareTo(a.key)

    要解决错误 2: 改变

    itemBuilder 函数中删除int index

    并且还在itemBuilder函数中的return之后添加new关键字。

    希望对您有所帮助!

    【讨论】:

    • 谢谢!错误 1 ​​已解决。对于错误 2,我仍然得到:The function expression type '(BuildContext, DataSnapshot, Animation&lt;double&gt;) → dynamic' isn't of type '(BuildContext, DataSnapshot, Animation&lt;double&gt;) → Widget'. This means its parameter or return type does not match what is expected. Consider changing parameter type(s) or the returned type(s). 所以,似乎是从 build() 返回 dynamic 而不是 Widget,对吗?
    • 当然你要返回一个小部件。它应该可以工作。代码似乎很好。能否也更新一下FirebaseAnimatedList的代码
    • 嘿伙计,您只是在itemBuilder 函数的返回语句中错过了new 关键字。
    • 更新了我的答案以包含它
    【解决方案2】:

    itemBuilder 有一个名为 index 的额外参数。

    typedef 小部件 FirebaseAnimatedListItemBuilder( BuildContext上下文, DataSnapshot 快照, 动画动画, 整数索引, );

    所以只需在动画后添加“,_”即可解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2021-10-30
      • 2021-11-10
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      相关资源
      最近更新 更多