【问题标题】:(flutter) FutureBuilder passing Map<> as property(颤振)FutureBuilder 将 Map<> 作为属性传递
【发布时间】:2021-12-23 21:50:24
【问题描述】:

我正在尝试从一本在 null 安全之前编写的书中学习 Flutter,如果我在文件开头添加 // @dart=2.9 ,其中的代码可以完美运行,但没有给出一些错误。

错误:

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

代码如下:

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)
          ),
        );
      },
    );
  }
}

所以我试着改变

builder: (context, comicResult) 

builder: (context, AsyncSnapshot<Map>comicResult)

现在错误是:

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

然后我尝试了

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

现在错误是:

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

我尝试在这里输入代码分别解决传递参数:

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

但我不知道如何传递图像

如何将Map&lt;String, dynamic&gt; 传递给ComicTile 类?

【问题讨论】:

  • comic: comicResult.data!这样传球怎么样
  • 可以为_fetchComic函数添加代码

标签: flutter android-studio dart dart-null-safety flutter-futurebuilder


【解决方案1】:

正如 Yeasin Sheikh 所建议的,这解决了这个问题:

comic: comicResult.data!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 2021-10-26
    • 2021-07-27
    • 2021-06-27
    • 2015-10-29
    相关资源
    最近更新 更多