Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart: mapping a list (list.map)

People also ask

How do you Map a dart list?

We can convert Dart List to Map in another way: forEach() method. var map2 = {}; list. forEach((customer) => map2[customer.name] = customer.

What is the difference between list and Map in Dart?

List, Set, Queue are iterable while Maps are not. Iterable collections can be changed i.e. their items can be modified, add, remove, can be accessed sequentially. The map doesn't extend iterable.

How do you list an object by another list of items in DART?

List<SomeClass> list = list to search; List<String> matchingList = list of strings that you want to match against; list. where((item) => matchingList. contains(item. relevantProperty));

What is Map () in Flutter?

Map<K, V> class Null safety. A collection of key/value pairs, from which you retrieve a value using its associated key. There is a finite number of keys in the map, and each key has exactly one value associated with it. Maps, and their keys and values, can be iterated.


you can use

moviesTitles.map((title) => Tab(text: title)).toList()

example:

    bottom: new TabBar(
      controller: _controller,
      isScrollable: true,
      tabs:
        moviesTitles.map((title) => Tab(text: title)).toList()
      ,
    ),

I'm new to flutter. I found that one can also achieve it this way.

 tabs: [
    for (var title in movieTitles) Tab(text: title)
  ]

Note: It requires dart sdk version to be >= 2.3.0, see here


Yes, You can do it this way too

 List<String> listTab = new List();
 map.forEach((key, val) {
  listTab.add(val);
 });

 //your widget//
 bottom: new TabBar(
  controller: _controller,
  isScrollable: true,
  tabs: listTab
  ,
),

I try this same method, but with a different list with more values in the function map. My problem was to forget a return statement. This is very important :)

 bottom: new TabBar(
      controller: _controller,
      isScrollable: true,
      tabs:
        moviesTitles.map((title) {return Tab(text: title)}).toList()
      ,
    ),

tabs: [...data.map((title) { return Text(title);}).toList(), extra_widget],

tabs: data.map((title) { return Text(title);}).toList(),

It's working fine for me