Using carousel_slider 1.0.0 I need to provide a map to define the amount of items in the slider.

Im able to find the amount of items coming from an API using 'articlesMap' but I need to convert this integer into List<Map> as the slider uses the following input:

items: [1,2,3,4,5].map((i) {})

How would I go about converting this? The amount of items the slider receives isn't static and thus I can't rely on inputting [1,2,3,4,5] where I may receive +/- 5 items.


Solution 1: Richard Heap

Dart provides a generator for lists, which you could use as:

items: List<int>.generate(articlesList.length, (i) => i + 1).map...

It would probably be simpler to just map you articles list (you don't show where you get the articles):

  CarouselSlider(
    height: 400.0,
    items: articlesList
        .map((a) => Builder(
              builder: (BuildContext context) => Container(
                    width: MediaQuery.of(context).size.width,
                    margin: EdgeInsets.symmetric(horizontal: 5.0),
                    decoration: BoxDecoration(color: Colors.amber),
                    child: Text(
                      a.name,
                      style: TextStyle(fontSize: 16.0),
                    ),
                  ),
            ))
        .toList(),
  );

If the articles are really in a map, you can convert to a list with .values