【问题标题】:Type 'List<Object>' is not a subtype of type 'List<String>'“List<Object>”类型不是“List<String>”类型的子类型
【发布时间】:2021-03-23 13:16:32
【问题描述】:

代码如图:

static const _modes = [
    ['KZTimer', 'SimpleKZ', 'Vanilla'],
    [128, 102, 64],
  ];

出错的部分是二维数组被调用到 ToggleButton() 类:

child: ToggleButton(_modes[0]),

ToggleButton 转到此构建方法,其中 _modes[0] 变为此处声明的 list

final List<String> list;

Widget build(BuildContext context) {
  return new Container(
    child: ToggleButtons(
      isSelected: _selections,
      onPressed:(int i){}

      children: <Widget>[
        ...(widget.list).map((str) {
          return Padding(
            padding: EdgeInsets.symmetric(horizontal: 12),
            child: Text(
              str,
              style: TextStyle(fontSize: 18),
            ),
          );
        }).toList(),

      ],
    ),
  );
}

模拟器变成红色:'List &lt;Object&gt;' 类型不是'List &lt;String&gt;' 类型的子类型

【问题讨论】:

    标签: flutter dart


    【解决方案1】:
    Update the final List<String> list; to either final List<dynamic> list; or final List list;
    Please refer below code
    
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
              visualDensity: VisualDensity.adaptivePlatformDensity,
            ),
            home: Settings());
      }
    }
    
    class Settings extends StatelessWidget {
      final String currentPage = 'Settings';
      static const modes = [
        ['KZTimer', 'SimpleKZ', 'Vanilla'],
        [128, 102, 64],
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SingleChildScrollView(
            child: Container(
              child: Padding(
                padding: EdgeInsets.all(8),
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      buildHeader(
                        title: 'Mode',
                        child: ToggleButton(list: modes[0]),
                      ),
                      SizedBox(height: 32),
                      buildHeader(
                        title: 'Tick rate',
                        child: ToggleButton(list: modes[1]),
                      ),
                      SizedBox(height: 32),
                      buildHeader(
                        title: 'Mode',
                        child: ToggleButton(list: modes[0]),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    Widget buildHeader({@required String title, @required Widget child}) {
      return Column(
        children: [
          Text(
            title,
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
          SizedBox(height: 16),
          child,
        ],
      );
    }
    
    class ToggleButton extends StatefulWidget {
      //Instead of this final List<String> list; update this to either final List<dynamic> list; or final List list;
      //final List<dynamic> list;
      final List list;
    
      const ToggleButton({Key key, this.list}) : super(key: key);
    
      @override
      State createState() => new _ToggleButtonState();
    }
    
    class _ToggleButtonState extends State<ToggleButton> {
      List<bool> _selections = [false, false, false];
    
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.blue.shade200,
          child: ToggleButtons(
            isSelected: _selections,
            fillColor: Colors.lightBlue,
            color: Colors.black,
            selectedColor: Colors.white,
            renderBorder: false,
            children: [
              ...(widget.list as List)?.map((answer) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: 12),
                  child: Text(
                    answer.toString() ?? '',
                    style: TextStyle(fontSize: 18),
                  ),
                );
              })?.toList(),
            ],
            onPressed: (int index) {
              setState(() {
                for (int i = 0; i < _selections.length; i++) {
                  if (index == i) {
                    _selections[i] = true;
                  } else {
                    _selections[i] = false;
                  }
                }
              });
            },
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      尝试将类型&lt;Widget&gt; 提供给map

            children: <Widget>[
              ...(widget.list).map<Widget>((str) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: 12),
                  child: Text(
                    str,
                    style: TextStyle(fontSize: 18),
                  ),
                );
              }).toList(),
      

      【讨论】:

      • 对不起,我弄错了,异常是在我调用类的地方捕获的
      【解决方案3】:

      错误可能发生在文本小部件中。 因为 str 不是 String 类型,它是 Object 类型。

       children: <Widget>[
              ...(widget.list).map<Widget>((str) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: 12),
                  child: Text(
                    str,
                    style: TextStyle(fontSize: 18),
                  ),
                );
              }).toList(),
      

      【讨论】:

      • 对不起,我弄错了,异常是在我调用类的地方捕获的
      猜你喜欢
      • 2021-09-11
      • 1970-01-01
      • 2021-07-06
      • 2021-02-17
      • 2021-07-17
      • 2020-12-04
      • 1970-01-01
      • 2019-11-07
      • 2021-12-30
      相关资源
      最近更新 更多