1

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!

1 Answers1

0

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
      )
  ..... 
Nisanth Reddy
  • 5,967
  • 1
  • 11
  • 29
  • you need to convert the `AsyncSnapshot` to the `QuerySnapshot` by calling `.data` on it. `snapshot.data` – Nisanth Reddy May 25 '21 at 04:13
  • thank you Nisanth for your answer. unfortunately the snapshot is from firebase, so I got this type in snapshot variable: 'AsyncSnapshot>' and when List myList = _list(snapshot); it say _list is not defined – Rafael Zablah May 25 '21 at 04:18
  • If `_list` is not defined then you dont have your `_list` function in the curent widget code. Post more in the question. – Nisanth Reddy May 25 '21 at 04:22
  • you're right Nisanth, I defined, but now the error is in the line List myList = _list(snapshot.data); : The expression doesn't evaluate to a function, so it can't be invoked. – Rafael Zablah May 25 '21 at 04:42
  • Use https://pastebin.com/ and share a link after pasting your full code. – Nisanth Reddy May 25 '21 at 04:45
  • thank you Nisante, the url is: https://pastebin.com/E4LkbTYU – Rafael Zablah May 25 '21 at 04:55
  • Check your question and check your code, you have changed the meaning of `_list1` and defined a new `_list2`. So call `List myList = _list2(snapshot.data);` – Nisanth Reddy May 25 '21 at 05:06
  • thank you Nisanth, I update the code, the url is: https://pastebin.com/u8X92Z7U - the error message now is: The argument type 'QuerySnapshot>?' can't be assigned to the parameter type 'QuerySnapshot>'. - I can't find where to change to nullable, any ideas? – Rafael Zablah May 25 '21 at 05:29
  • Change it to this, ` List _list2( QuerySnapshot? productSnapshot) { return productSnapshot!.docs` – Nisanth Reddy May 25 '21 at 05:32
  • you rock man, it work perfectly!, thank you again for all your help, regards – Rafael Zablah May 25 '21 at 05:39
  • Glad to have helped :) Do consider accepting and upvoting my answer – Nisanth Reddy May 25 '21 at 05:40
  • of course, is accepted :), but I don't have the ability yet to vote, (I lost my old account), when I access, I Will, thank you Nisanth and hello from Chile :) – Rafael Zablah May 25 '21 at 05:53
  • That's alright. Have a wonderful journey with Flutter. :) – Nisanth Reddy May 25 '21 at 05:56