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 get the length of list elements if the list contains Widget types? (flutter)

I cannot get the length of all my elements in my list because it contains Widgets. Is there a way I can get around this? I use following code to generate the list with the element lenghts.

I’m getting following error: The element type 'List<Container>' can't be assigned to the list type 'Widget'.dartlist_element_type_not_assignable.

This is an example of the contents list I’m trying to get the length from: Contents list

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

I’ve tried list builder instead of list generator but this doesn’t seem to make a difference.

>Solution :

Add spread operator at the front if the List, and use the length on the List normally.

children: [
...List.generate(contents.length, (index) => buildDot(index, context)), // add ... like this in your List
],

this will lead that your list will be merged with the principal list.

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