简体   繁体   English

将列表中的文本设置为文本小部件 flutter

[英]Set text from list to Text Widget flutter

I'm trying to set quotes from list to Text widget but i am facing this problem我正在尝试将列表中的引号设置为文本小部件,但我遇到了这个问题

The argument type 'List<Iterable>' can't be assigned to the parameter type 'List'.不能将参数类型“List<Iterable>”分配给参数类型“List”。

this is my code这是我的代码

import 'package:flutter/material.dart';

void main() {
  runApp(myApp());
}

class myApp extends StatefulWidget {
  @override
  _myAppState createState() => _myAppState();
}

class _myAppState extends State<myApp> {
  List<String> quotesList = [
    "The secret of getting ahead is getting started",
    "Only the paranoid survive",
    "It’s hard to beat a person who never gives up.",
    "Never Had luck never needed it"
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.black38,
          title: Text("Quotes"),
        ),
        body: Column(
          children: [quotesList.map((e) => Text(e))].toList(),
        ),
      ),
    );
  }
}

You don't need to wrap the list with '[' and ']'您不需要用 '[' 和 ']' 包装列表

Column(
   children: quotesList.map((e) => Text(e)).toList(),
),

And if you want to add more widgets, you can use like this如果你想添加更多的小部件,你可以像这样使用

Column(
    children: quotesList.map<Widget>((e) => Text(e)).toList()
        ..addAll([
            Container() //You can use addAll method and add some juicy widgets
    ]),
),

Remove [] from the quotesList -从引号列表中删除 [] -

quotesList.map((e) => Text(e)).toList(),

This might fix your issue -这可能会解决您的问题 -

import 'package:flutter/material.dart';

void main() {
  runApp(myApp());
}

class myApp extends StatefulWidget {
  @override
  _myAppState createState() => _myAppState();
}

class _myAppState extends State<myApp> {
  List<String> quotesList = [
    "The secret of getting ahead is getting started",
    "Only the paranoid survive",
    "It’s hard to beat a person who never gives up.",
    "Never Had luck never needed it"
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.black38,
          title: Text("Quotes"),
        ),
        body: Column(
          children: quotesList.map((e) => Text(e)).toList(),
        ),
      ),
    );
  }
}

To answer your question, you need to remove the [ and ] , to look like this要回答您的问题,您需要删除[] ,看起来像这样

quotesList.map((e) => Text(e)).toList()

If you want to add more widgets, you can use the spread operator如果要添加更多小部件,可以使用扩展运算符

Column(
  children: <Widget>[
    // you can add more widgets here
    ...quotesList.map((e) => Text(e)),
    // you can add more widgets here too
  ],
)

Here is another (easy) approach.这是另一种(简单的)方法。

Add this function to your current class -将此 function 添加到您当前的 class -

List<Widget> getTextWidgets() {
  List<Widget> _widgets = [];
  for(int i=0;i<quotesList.length;i++) {
    _widgets.add(
      Text(quotesList[i])
    );
  }
  return _widgets;
}

And simply call it like -简单地称之为 -

body: Column(
  children: getTextWidgets(),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM