参数类型 'Iterable<dynamic>' 无法分配给参数类型 'List<Widget>'
The argument type 'Iterable<dynamic>' can't be assigned to the parameter type 'List<Widget>'
大家好,我正在尝试在我的代码中映射一个字符串列表,但出现错误“参数类型 'Iterable' 无法分配给参数类型 'List'."。我正在尝试重现下图中的结果。这是下面的代码;
**quote.dart**
class Quote {
String? text;
String? author;
Quote({ required this.text,required this.author });
}
**main.dart**
void main() {
runApp(MaterialApp(home: QuoteList()));
}
class QuoteList extends StatefulWidget {
const QuoteList({Key? key}) : super(key: key);
@override
State<QuoteList> createState() => _QuoteListState();
}
class _QuoteListState extends State<QuoteList> {
@override
List<dynamic> quotes = [
//"Quote" is the the name of the class of the second dart file.
Quote(
author: "Eminem",
text:
"You better lose yourself in music the moment you own it never let it go"),
Quote(
author: "Walt Disney",
text: "The way to get started is to quit talking and begin doing."),
Quote(
author: "Mother Teresa",
text:
"Spread love everywhere you go. Let no one ever come to you without leaving happier")
//
// "
// ". -"
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text("Awesome Killer Quotes",
style: TextStyle(
color: Colors.grey[700],
)),
centerTitle: true,
backgroundColor: Colors.yellowAccent[400],
),
body: Column(
children: quotes.map(children: quotes.map((quote) => Text('${quote.text} -
${quote.author}')).toList(),
// (quote) => Text(quote.text + "-" + quote.author).toList(),
// ((quote) => quoteTemplate(quote).toList()),
),
//
//return Text('>>'+quote.text+', '+quote.author);
),
);
}
}
如果有人能帮助我,我将不胜感激P.S错误在main.dart文件中
Expected result of the code
所以试试这个
List<Quote> quotes = [
//"Quote" is the the name of the class of the second dart file.
Quote(
author: "Eminem",
text:
"You better lose yourself in music the moment you own it never let it go"),
Quote(
author: "Walt Disney",
text: "The way to get started is to quit talking and begin doing."),
Quote(
author: "Mother Teresa",
text:
"Spread love everywhere you go. Let no one ever come to you without leaving happier")
];
作为您的列表
然后是您的小部件
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text("Awesome Killer Quotes",
style: TextStyle(
color: Colors.grey[700],
)),
centerTitle: true,
backgroundColor: Colors.yellowAccent[400],
),
body: Column(
children: quotes.map((e)
=>Text('${e.text}')
).toList(),
),
);
保持简单
解决方案:
Column(
children: quotes.map((quote) => Text('${quote.text} '
'- ${quote.author}')).toList()
);
大家好,我正在尝试在我的代码中映射一个字符串列表,但出现错误“参数类型 'Iterable' 无法分配给参数类型 'List'."。我正在尝试重现下图中的结果。这是下面的代码;
**quote.dart**
class Quote {
String? text;
String? author;
Quote({ required this.text,required this.author });
}
**main.dart**
void main() {
runApp(MaterialApp(home: QuoteList()));
}
class QuoteList extends StatefulWidget {
const QuoteList({Key? key}) : super(key: key);
@override
State<QuoteList> createState() => _QuoteListState();
}
class _QuoteListState extends State<QuoteList> {
@override
List<dynamic> quotes = [
//"Quote" is the the name of the class of the second dart file.
Quote(
author: "Eminem",
text:
"You better lose yourself in music the moment you own it never let it go"),
Quote(
author: "Walt Disney",
text: "The way to get started is to quit talking and begin doing."),
Quote(
author: "Mother Teresa",
text:
"Spread love everywhere you go. Let no one ever come to you without leaving happier")
//
// "
// ". -"
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text("Awesome Killer Quotes",
style: TextStyle(
color: Colors.grey[700],
)),
centerTitle: true,
backgroundColor: Colors.yellowAccent[400],
),
body: Column(
children: quotes.map(children: quotes.map((quote) => Text('${quote.text} -
${quote.author}')).toList(),
// (quote) => Text(quote.text + "-" + quote.author).toList(),
// ((quote) => quoteTemplate(quote).toList()),
),
//
//return Text('>>'+quote.text+', '+quote.author);
),
);
}
}
如果有人能帮助我,我将不胜感激P.S错误在main.dart文件中
Expected result of the code
所以试试这个
List<Quote> quotes = [
//"Quote" is the the name of the class of the second dart file.
Quote(
author: "Eminem",
text:
"You better lose yourself in music the moment you own it never let it go"),
Quote(
author: "Walt Disney",
text: "The way to get started is to quit talking and begin doing."),
Quote(
author: "Mother Teresa",
text:
"Spread love everywhere you go. Let no one ever come to you without leaving happier")
];
作为您的列表
然后是您的小部件
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text("Awesome Killer Quotes",
style: TextStyle(
color: Colors.grey[700],
)),
centerTitle: true,
backgroundColor: Colors.yellowAccent[400],
),
body: Column(
children: quotes.map((e)
=>Text('${e.text}')
).toList(),
),
);
保持简单
解决方案:
Column(
children: quotes.map((quote) => Text('${quote.text} '
'- ${quote.author}')).toList()
);