flutterflutter2.0

how to convert _list(QuerySnapshot<Map<String, dynamic>> snapshot) into List<dynamic> in flutter 2.0


I'm trying to get a List<dynamic'> _list but since I upgraded flutter to 2.0, I had to change my code to _list(QuerySnapshot<Map<String, dynamic>> snapshot). that type don't have length, or elementAt like normal List<dynamic'> type. it is possible to convert it?

my code:

List _list(QuerySnapshot<Map<String, dynamic>> snapshot) {
                              return snapshot.docs
                                  .map((doc) => new Brand(
                                        doc.data()['brandId'].toString(),
                                      ))
                                  .toList();
                            }

later on my code I need use some fields:

return StaggeredGridView.countBuilder(
                              itemCount: _list.length,
                              itemBuilder: (BuildContext context, int index) {
                                Brand brand = _list.elementAt(index) as Brand;
                                return InkWell(
                                  onTap: () {
                                    Navigator.of(context).pushNamed('/Brand',
                                        arguments: new RouteArgument(
                                            id: _list[index].id)

the error message: error: The operator '[]' isn't defined for the type 'List Function(QuerySnapshot<Map<String, dynamic>>)'

any ideas are welcome, thank's in advance!


Solution

  • as of now your _list is defined as a Function that returns a List rather than being a List itself.

    So, in order to use it, you should firstly properly call it with the necessary parameter.

    Like this,

    List myList = _list(snapshot);  // I assume you have access to a snapshot variable
    return StaggeredGridView.countBuilder(
      itemCount: myList .length,
      itemBuilder: (BuildContext context, int index) {
      Brand brand = myList.elementAt(index) as Brand;
      return InkWell(
        onTap: () {
          Navigator.of(context).pushNamed('/Brand',
            arguments: new RouteArgument(
            id: myList[index].id
          )
      .....