2

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'.

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(),
        ),
      ),
    );
  }
}
2
  • just remove [] in children ,like this children: quotesList.map((e) => Text(e)).toList(), Commented May 21, 2021 at 14:41
  • another thing the class name should be with upperCase in First Lettre, myApp => MyApp :) Commented May 21, 2021 at 14:43

4 Answers 4

4

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
    ]),
),
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it 's working , but what if i want to add more widgets in this column ?
You can use the spread operator within your column, it looks a lot cleaner and you don't have to use an add all method
1

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

Comments

1

Here is another (easy) approach.

Add this function to your current 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(),
),

Comments

0

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
  ],
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.