Home > OS >  Is there a way to solve NoSuchMethodError originating from indexedStack when using a list in flutter
Is there a way to solve NoSuchMethodError originating from indexedStack when using a list in flutter

Time:01-18

The indexedStack I use to show a series of pictures one after the other is the origination of a noSuchMethod error. I am trying to build the stack using a list in my firebase like this:enter image description here

The relevant code that causes the error is here; channelData is defined as channelData = Map<String, dynamic>.from(widget.channelSnapshot.data() as Map); where "posts" is the list that holds the sublists with the timestamps and links.

                  IndexedStack(
                    index: index,
                    children: channelData["posts"]
                        .toList()((doc) {
                          precacheImage(
                              NetworkImage(doc["postPicUrl"]), context);
                          return Padding(
                            padding: const EdgeInsets.all(0.0),
                            child: Container(
                                width: 280,
                                height: 150,
                                padding: EdgeInsets.all(7),
                                decoration: BoxDecoration(
                                  image: DecorationImage(
                                      image: NetworkImage(doc["postPicUrl"]),
                                      fit: BoxFit.cover),
                                )),
                          );
                        }).toList(),
                  )

The error code is here:

  Dynamic call of object has no instance method 'call'.
  Receiver: Instance of 'List<dynamic>'
  Arguments: [Instance of '(dynamic) => Padding']
  The relevant error-causing widget was:
  Builder

I know that the issue is probably how I'm presenting the list to the indexedStack. The code was working when I was doing the same thing with snapshot.data!.docs.map((doc) as the children of the indexedStack instead of channelData[posts]. I know that indexedstack is the source of the issue because when it is removed the page loads fine, including the widgets also dependent on data from that list. I have tried changing the location of the .toList(); changing which widget is shown first, etc, but there really aren't any similar issues on the internet. Thanks so much in advance for any help.

CodePudding user response:

Problem:

You're missing the .map() statement before the ((doc) {...}).toList() callback.

From your current code, you're attempting to call a method on the List which is why the error message says:

Dynamic call of object has no instance method 'call'.
Receiver: Instance of 'List<dynamic>'

Solution:

Map the List by calling .map() on the list and passing it the callback.

Change block of code:

IndexedStack(
  index: index,
  children: channelData["posts"]
    .toList()((doc) {
      ...                          
    }).toList(),
)

to this:

IndexedStack(
  index: index,
  children: channelData["posts"]
    .toList().map<Widget>((doc) {
      ...                          
    }).toList(),
)
  • Related