Reputation: 10780
I know that there are other ways of achieving this, but I want to use a Future via initState() to obtain a List using SharedPreferences. The following illustrates what I want to achieve, but it does not work as required, because the Future returns immediately before completing the task.
How should this be structured?
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class _MyHomePageState extends State<MyHomePage> {
SharedPreferences _prefs;
String _sMessage = "No response from getting params";
List<String> _lsCategories;
@override
void initState() {
super.initState();
_initPreferences().then((_) {
try {
_lsCategories = _prefs.getStringList("categories") ?? [];
debugPrint("Categories = $_lsCategories");
_sMessage = "Categories loaded OK";
} catch (vError) {
_sMessage = ("Error loading categories = $vError");
}
setState(() {});
});
}
Future<void> _initPreferences() async {
SharedPreferences.getInstance().then((prefs) {
_prefs = prefs;
debugPrint("Preferences initialized OK");
}).catchError((vError) {
debugPrint("Error initializing preferences = ${vError.toString()}");
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_sMessage,
style: TextStyle(
color: Colors.red[500],
fontWeight: FontWeight.bold,
fontSize: 20)),
],
),
),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Future Test',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: "Flutter Future Test"),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
Upvotes: 0
Views: 121
Reputation: 10881
Change your _initPreferences
method to the following:
Future<void> _initPreferences() async {
try {
final prefs = await SharedPreferences.getInstance();
_prefs = prefs;
debugPrint("Preferences initialized OK");
} catch (e) {
debugPrint("Error initializing preferences = ${e.toString()}");
}
}
The issue is that when using .then()
on a Future you are not waiting for that future to finish, using await
does wait.
Upvotes: 1
Reputation: 4253
Try it.
Future<void> _initPreferences() async {
_prefs = await SharedPreferences.getInstance().catchError((vError) {
debugPrint("Error initializing preferences = ${vError.toString()}");
};
debugPrint("Preferences initialized OK");
}
Upvotes: 0