the element type 'List' can't be assigned to the list type 'Widget'

前端 未结 4 1151
死守一世寂寞
死守一世寂寞 2021-02-05 03:19

I am trying to add data using for loop in gridview but it is showing some error. Here is my code for component

return new GridView.count(
    crossAxisCount: 2,
         


        
4条回答
  •  清歌不尽
    2021-02-05 03:39

    There are two methods I found working.

    Method 1 - Simple

    This method is supported in the latest version of flutter

    var list = ["one", "two", "three", "four"]; 
    
    child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
              for(var item in list ) Text(item)
          ],
    ),
    

    Method 2 - A bit Complex

    Tested in Flutter 1.12.13+hotfix.7 and Dart 2.7.0, we can iterate over the list of objects as follows:

    Iterate over the list of objects

    ...data.map((f) => paragraph(f.title, f.description)).toList(),
    

    Custom widget

    Column paragraph(String title, String description) {
      return new Column(
        children: [
          Container(
            child: Text(title),
          ),
          Container(
            child: Text(description),
          ),
        ],
      );
    }
    

    Data: List of objects

    List data = [
        new AboutUsData(
          title: 'Our Vision',
          description:
              'OUR VISION',
        ),
        new AboutUsData(
          title: 'Our Mission',
          description:
              'OUR MISSION',
        ),
        new AboutUsData(
          title: 'Our Values',
          description:
              'As we grow as a company',
        ),
      ];
    

提交回复
热议问题