参数类型“Iterable”不能分配给参数类型“List”;

发布于 2025-01-13 09:26:25 字数 1886 浏览 5 评论 0原文

大家好,我正在尝试在代码中映射字符串列表,但它给了我错误“参数类型‘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);
      ),
    );
  }
}

如果有人可以帮助我,我将非常感激

Hi guys please i'm trying to map a list of strings in my code but its is give me the error "The argument type 'Iterable' can't be assigned to the parameter type 'List'.". I'm trying to reproduce the result in the picture below. Here is the code below;

**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);
      ),
    );
  }
}

If anybody can help me I will really appreciate it???????? P.S the error is in the main.dart file

Expected result of the code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带上头具痛哭 2025-01-20 09:26:25

保持简单

解决方案:

Column(
    children: quotes.map((quote) => Text('${quote.text} '
        '- ${quote.author}')).toList()
    );

just keep it simple

Solution:

Column(
    children: quotes.map((quote) => Text('${quote.text} '
        '- ${quote.author}')).toList()
    );

鼻尖触碰 2025-01-20 09:26:25

所以尝试将此

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(),
     ),
    );

so try this

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")
  ];

as your list

then your widget

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(),
     ),
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文