Тип флаттера "List<dynamic>" не является подтипом типа "list<Widget>"
Я пытаюсь составить список с картами с изображением, идея состоит в том, что эти карты будут генерироваться динамически в будущем через запросы 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)),
)
],
)
)
],
),
);
}
}
1 ответ
Решение
+ Изменить
List cards = new List.generate(20, (i)=>new CustomCard());
в
final List<Widget> cards = List<Widget>.generate(20, (i)=>new CustomCard());
Дети ожидают от List ничего другого, даже динамического типа.