【发布时间】:2020-11-06 00:33:09
【问题描述】:
我正在尝试使用 SharedPreferences 获得的值创建一个列表。但是,如果我想使用 SharedPreference,我必须使用异步方法/未来类型,我不知道如何使用它们。我尝试遍历sports 列表,但它是异步/未来的。那么如何迭代它(使用地图)?
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
Center(
child: Align(
alignment: Alignment.bottomCenter,
child: CarouselSlider(
options: CarouselOptions(
height: 565,
aspectRatio: 16/9,
enlargeCenterPage: true,
viewportFraction: .8,
),
items: sportsList.map((i) {
return Builder(builder: (BuildContext context) {
return Container(
.......
.........
.......
}
static Future<bool> initSharedPreference(String key) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool(key) ?? false;
}
Future<List> sportsList() async {
List sports=[
await initSharedPreference("test")),
....,
....,
....
];
}
由于以下原因,此代码不起作用:
The method 'map' isn't defined for the type 'Future'.
【问题讨论】:
标签: flutter asynchronous dart future