Dart: mapping a list (list.map)

后端 未结 5 1746
后悔当初
后悔当初 2020-12-29 00:37

I have a list of Strings, e.g.,

var moviesTitles = [\'Inception\', \'Heat\', \'Spider Man\'];

and wanted to use moviesT

相关标签:
5条回答
  • 2020-12-29 01:07

    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

    0 讨论(0)
  • 2020-12-29 01:08

    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()
          ,
        ),
    
    0 讨论(0)
  • 2020-12-29 01:12

    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()
          ,
        ),
    
    0 讨论(0)
  • 2020-12-29 01:15

    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
      ,
    ),
    
    0 讨论(0)
  • 2020-12-29 01:33
    ...data.map((title) { return Text(title);}).toList(),
    

    It's working fine for me

    0 讨论(0)
提交回复
热议问题