0

I'm trying to learn Flutter from a book that has been written before null safety, the code in it works perfectly if I add // @dart=2.9 at the beginning of the file, but gives some error without.

The error:

The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'.

Here is the code:

ListView.builder(
        itemCount: latestComic,
        itemBuilder: (context, i) => FutureBuilder(
          future: _fetchComic(i),
          builder: (context, comicResult) =>
          comicResult.hasData ?
          ComicTile(comic: comicResult.data) : // <- here gives the error when I remove 
                                               // @dart=2.9
              Center(
                  child: Column(
                    children: [
                      Divider(
                        height: 40,
                      ),
                      CircularProgressIndicator()
                    ],
                  )
              )
        ),
      ),

class ComicTile extends StatelessWidget {
  final Map<String, dynamic> comic;
  ComicTile({required this.comic});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Image.network(
        comic["img"],
        height:  50,
          width: 50,
      ),
      title: Text(comic["title"]),
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (BuildContext context) => ComicPage(comic: comic)
          ),
        );
      },
    );
  }
}

So I tried to change

builder: (context, comicResult) 

to

builder: (context, AsyncSnapshot<Map>comicResult)

and now the error is:

The argument type 'Map<dynamic, dynamic>?' can't be assigned to the parameter type 'Map<String, dynamic>'.

Then I tried

builder: (context, AsyncSnapshot<Map<String, dynamic>>comicResult)

and now the error is:

The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, dynamic>'.

I tried to enter code here to solve passing parameters separately:

ComicTile(comicTitle: comicResult.data!["title"])

But I don't know how to pass images

How to pass Map<String, dynamic> to the ComicTile class?

Amir
  • 362
  • 2
  • 10
Peppinhood
  • 39
  • 9

1 Answers1

0

As suggested by Yeasin Sheikh this solves the issue:

comic: comicResult.data!
Peppinhood
  • 39
  • 9