1

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

“List”类型不是“list”类型的子类型

我该如何解决?,请您的帮助。

我的代码在一个名为:

请求.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)),
                             )

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

1 回答 1

6

改变

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

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

Children 不期望 List 别的,甚至不期望动态类型。

于 2019-03-20T14:17:24.807 回答