I'm trying to create a list with values obtained by SharedPreferences. However, if I want to use SharedPreference, I've to use async methods / future types and I have no idea how to work with them. I try to iterate over the sports list, but it is asynchronous/future. So how do I iterate over it (using map)?
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")),
....,
....,
....
];
}
This code doesn't work because of the following:
The method 'map' isn't defined for the type 'Future'.