【问题标题】:The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'. What am I doing wrong? Baby Flutterer Here参数类型“List<dynamic>”不能分配给参数类型“List<Widget>”。我究竟做错了什么?小飘飘在这里
【发布时间】:2022-01-04 07:08:30
【问题描述】:

我正在尝试创建User 类型的列表,如果该列表不为空,请提供列表中的下一个用户。我已经阅读了尽可能多的 Flutter 文档,但我不知所措。

class _SwipePageState extends State<SwipePage> implements PreferredSizeWidget {
  getUsers() async {
    Box box = await Hive.openBox('usersBox');
    swipableUsers = box.values.toList();
  }

  List swipableUsers = <User>[];
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: PreferredSize(
          preferredSize: const Size.fromHeight(100),
          child: Container(color: Colors.transparent),
        ),
        body: Padding(
          padding: const EdgeInsets.all(8),
          child: Column(
            children: [
              swipableUsers.isEmpty
                  ? const Text('No more users')
                  : Stack(children: swipableUsers.map(buildUser).toList()),
              Expanded(child: Container()),
              const BottomButtonsWidget()
            ],
          ),
        ),
      );

  Widget buildUser(User currentUser) {
    int userIndex = swipableUsers.indexOf(currentUser);
    bool isUserInFocus = userIndex == swipableUsers.length - 1;
    //Do other stuff with currentUser

错误在: Stack(children: swipableUsers.map(buildUser).toList()),,错误是这个问题的标题,The argument type 'List&lt;dynamic&gt;' can't be assigned to the parameter type 'List&lt;Widget&gt;'

有没有机会拯救我的小应用程序,还是我纠结的太多以至于没有希望了?提前感谢您的回答:)

【问题讨论】:

  • 列出swipableUsers = [];这是用户数据类型列表,你想在这里放置小部件列表尝试进行更改

标签: list flutter dart box flutter-hive


【解决方案1】:

您需要使用 swipableUsers 变量生成一个小部件列表,但您将用户列表返回给 Stack 的 children 属性。试试这个:

Stack(children: List.generate(swipableUsers.length,(index)=>buildUser(swipableUsers[index])))

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2021-07-04
    • 2021-09-15
    • 2021-10-18
    • 1970-01-01
    • 2021-09-13
    • 2019-10-05
    • 2021-11-13
    • 2021-12-24
    相关资源
    最近更新 更多