Anas Hafidi
Anas Hafidi

Reputation: 425

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

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

Upvotes: 2

Views: 4557

Answers (4)

robben
robben

Reputation: 785

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

Upvotes: 1

mrgnhnt96
mrgnhnt96

Reputation: 3945

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

Upvotes: 0

Ulaş Kasım
Ulaş Kasım

Reputation: 870

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

Upvotes: 4

Thaanu
Thaanu

Reputation: 85

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

Upvotes: 0

Related Questions