Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to map array of objects in flutter?

Hello I’m trying to map array of objects and return widget with some value of given objects… Like this

 _metapodaci?.map<Widget>((e) => Text("Naziv dokumenta je ${e.label}")),

I get an error saying "The element type ‘Iterable’ can’t be assigned to the list type ‘Widget"
but when I map inside text widget like this:

 Text("Meta podaci: ${_metapodaci.map((e) => e.label)}"),

then _metapodaci are mapped good.
Here is Whole widget where I’m trying to map if needed:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 Padding(
           padding: const EdgeInsets.all(8.0),
            
            child:   Expanded(
              child: _metapodaci.isEmpty ? 
              Text("Odaberite vrstu dokumenta") :
              Column(
                children: [
                  Text("Meta podaci: ${_metapodaci.map((e) => e.label)}"),
                  //_metapodaci.map<Widget>((e) => Text("Naziv dokumenta je ${e.label}")),
                ],
              ),
            ),
          ),

Appreciate if someone can advise. Thank you in advance!

>Solution :

You need to convert your array to list of widget and also for null safety issue you need check for null before too , try this:

Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Expanded(
                    child: _metapodaci == null || _metapodaci!.isEmpty
                        ? Text("Odaberite vrstu dokumenta")
                        : Column(
                            children: _metapodaci!
                                .map<Widget>((e) =>
                                    Text("Naziv dokumenta je ${e.label}"))
                                .toList(),
                          ),
                  ),
                ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading