【问题标题】:Set text from list to Text Widget flutter将列表中的文本设置为文本小部件颤动
【发布时间】:2021-08-10 19:22:08
【问题描述】:

我正在尝试将列表中的引号设置为文本小部件,但我遇到了这个问题

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

这是我的代码

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

【问题讨论】:

  • 只需删除子项中的 [],就像这样子项:quotesList.map((e) => Text(e)).toList(),
  • 在First Lettre中类名应该是大写,myApp => MyApp :)

标签: list flutter dart


【解决方案1】:

这是另一种(简单的)方法。

将此功能添加到您当前的课程中 -

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

然后简单地称它为 -

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

【讨论】:

    【解决方案2】:

    要回答您的问题,您需要删除 [],使其看起来像这样

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

    如果要添加更多小部件,可以使用扩展运算符

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

    【讨论】:

      【解决方案3】:

      你不需要用'['和']'来包装列表

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

      如果你想添加更多的小部件,你可以像这样使用

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

      【讨论】:

      • 感谢它的工作,但如果我想在这个专栏中添加更多小部件怎么办?
      • 您可以在您的列中使用扩展运算符,它看起来更干净,您不必使用添加所有方法
      【解决方案4】:

      从引号列表中删除 [] -

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

      这可能会解决您的问题 -

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

      【讨论】:

        猜你喜欢
        • 2018-11-18
        • 1970-01-01
        • 2019-11-13
        • 1970-01-01
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        • 1970-01-01
        相关资源
        最近更新 更多