You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何在Row内使用List?出现类型赋值错误如何解决?

报错详情

lib/main.dart:94:21: Error: The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'.
- 'List' is from 'dart:core'.
- 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/fluttersdk/flutter/packages/flutter/lib/src/widgets/framework.dart').

触发报错代码:children: scoreKeeper,


报错原因

Dart为强类型语言,Row组件的children参数仅接受List<Widget>类型的入参,你定义的scoreKeeper列表未指定元素类型,默认被推断为List<dynamic>,类型不匹配导致赋值失败。


修复方案

  • 定义列表时明确指定元素类型为Widget,这是最推荐的修复方式:
    // 原错误声明(默认动态类型)
    List scoreKeeper = [];
    // 修改后声明
    List<Widget> scoreKeeper = [];
    
    注意后续往scoreKeeper中添加的所有元素都必须是Widget子类(比如Icon、Text、Container等),不能存入字符串、数字等非Widget类型值。
  • 临时类型转换(仅适合临时调试用,不建议线上代码使用):
    children: scoreKeeper.cast<Widget>(),
    

内容的提问来源于stack exchange,提问作者Logic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 15:54:06