0

I have an array that has an image URL and normal string as its content. I want to display an image or Text widget based on which value is being read. I have used this to make that work:

Widget build(BuildContext context) {
    final usrMap = {"tom", 'tom.png', "taommy"};
    return MaterialApp(
      title: 'Flutter Tutorial',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Text Widget Tutorial'),
        ),
        body: Column(
          children: [
            for( var prop in usrMap){
              prop.contains(".com")? Text(prop) : Text("not .png")
            },
        ]
         ),
      ),
    );
  }

I am getting this error The element type 'Set' can't be assigned to the list type 'Widget' How do I fix this?

  • 1
    You don't need the curly brackets for for loops inside lists as they can't be multiline anyways. Remove those and it should work: `for (var prop in userMap) props.contains(".com") ? Text(prop) : Text("not .png")`. Here's a [SO longer answer](https://stackoverflow.com/a/58039765/8213910) on several more ways to add values to a list in case its useful – Nolence Jul 20 '21 at 16:13

1 Answers1

0

use like this.

 Widget build(BuildContext context) {
    final usrMap = {
      "tom",
      'tom.png',
      "taommy",
      "sheikh.com",
    };
    return MaterialApp(
      title: 'Flutter Tutorial',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Text Widget Tutorial'),
        ),
        body: Column(children: [
          ...usrMap.map((e) {
            if (e.contains(".com"))
              return Text("prop");
            else
              return Text("not .png");
          }).toList(),
        ]),
      ),
    );
  }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56