我在 Flutter itemBuilder 中不断收到此错误

时间:2021-04-10 06:11:54

标签: firebase flutter dart firebase-realtime-database

这在 android studio 中显示 构造函数返回类型 'dynamic',它不是预期的类型 'Widget' 并且还给了我这个参考 package:flutter/src/material /scaffold.dart

body: new Container(
          child: new Column(
            children: <Widget>[
              new Flexible(
                child: new FirebaseAnimatedList(
                  query: reference,
                  padding: const EdgeInsets.all(8.0),
                  reverse: true,
                  sort: (a, b) => b.key.compareTo(a.key),
                  itemBuilder: (_, DataSnapshot messageSnapshot, Animation<double> animation, int index) {
                    return new ChatTextController(
                      messageSnapshot: messageSnapshot,
                      animation: animation,
                    );
                  },
                ),
              ),
              new Divider(height: 1.0),
              new Container(
                decoration:
                new BoxDecoration(color: Theme.of(context).cardColor),
                child: _buildTextComposer(),
              ),
              new Builder(builder: (BuildContext context) {
                _scaffoldContext = context;
                return new Container(width: 0.0, height: 0.0);
              })
            ],
          ),
          decoration: Theme.of(context).platform == TargetPlatform.android
              ? new BoxDecoration(
              border: new Border(
                  top: new BorderSide(
                    color: Colors.grey[200],
                  )))
              : null,
        )

1 个答案:

答案 0 :(得分:0)

确保这个控制器返回一个像 Container 之类的 Widget 而不是动态值:

const arr = [0, 4, 10, -20, 10, 20, 50];
let res = [];

arr.forEach((v, i) => res = i > 0 ? res.concat([(v + arr[i - 1]) / 2, v]) : [v]);

console.log(res);

ListView 的 itemBuilder 属性总是期望一个由其构建器函数返回的 UI 小部件