【发布时间】: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<String, dynamic> 传递给ComicTile 类?
【问题讨论】:
-
像
comic: comicResult.data!这样传球怎么样 -
可以为
_fetchComic函数添加代码
标签: flutter android-studio dart dart-null-safety flutter-futurebuilder