By Alvin Alexander. Last updated: October 24, 2019
When I was trying to debug a problem that I thought was related to Flutter’s SharedPreferences
library, I ended up rewriting a preferences method to use code like this rather than the normal approach:
Future<SharedPreferences> fPrefs = SharedPreferences.getInstance(); fPrefs.then((value) {rez = value.getBool(KEY_ENABLE_NOTIFICATIONS) ?? false; }) .catchError((e) { debugPrint("===== ERROR: ${e.error}"); return 60; }); return rez;
While that ended up being a waste of time, the benefit of my side excursion is that I get to show this example of how to use then
and catchError
with a Dart future. So if you wanted to see a Dart Future/then/catchError example, I hope this is helpful.