【问题标题】:Flutter type 'List<dynamic>' is not a subtype of type 'list<Widget>'Flutter 类型 'List<dynamic>' 不是类型 'list<Widget>' 的子类型
【发布时间】:2019-03-20 14:09:58
【问题描述】:

我正在尝试使用带有图像的卡片制作一个列表,其想法是这些卡片是将来通过 API 请求动态生成的,但现在我只想从图像开始,但是当我运行时出现以下错误:

类型“列表”不是类型“列表”的子类型

我该如何解决?请您帮忙。

我的代码在一个名为:

requests.dart

这是我的代码:

import 'package:flutter/material.dart';

  class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home:new MyCard()
    );
  }
}

class MyCard extends StatelessWidget{
  List cards = new List.generate(20, (i)=>new CustomCard());
  @override
  Widget build(BuildContext context) {
      return new Scaffold(
            appBar: new AppBar(
              title: new Text('Car Results'),
              backgroundColor:new Color(0xFF673AB7),
            ),
            body: new Container(
              child: new ListView(
                children: cards,
              )
            )
        );

    }
}

class CustomCard extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
              return  new Card(
                    child: new Column(
                      children: <Widget>[
                        new Image.network('https://matwrites.com/wp-content/uploads/2018/03/Flutter.png'),
                        new Padding(
                          padding: new EdgeInsets.all(7.0),
                          child: new Row(
                            children: <Widget>[
                             new Padding(
                               padding: new EdgeInsets.all(7.0),
                               child: new Icon(Icons.thumb_up),
                             ),
                             new Padding(
                               padding: new EdgeInsets.all(7.0),
                               child: new Text('Like',style: new TextStyle(fontSize: 18.0),),
                             ),
                             new Padding(
                               padding: new EdgeInsets.all(7.0),
                               child: new Icon(Icons.comment),
                             ),
                             new Padding(
                               padding: new EdgeInsets.all(7.0),
                               child: new Text('Comments',style: new TextStyle(fontSize: 18.0)),
                             )

                            ],
                          )
                        )
                      ],
                    ),
                  );
  }
}

【问题讨论】:

    标签: listview dart flutter statelesswidget


    【解决方案1】:

    改变

    List cards = new List.generate(20, (i)=>new CustomCard());
    

    final List<Widget> cards = List<Widget>.generate(20, (i)=>new CustomCard());
    

    孩子们不希望 List 有别的东西,甚至是动态类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 2021-05-17
      • 2018-09-11
      • 2020-12-10
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多