I'm attempting to use this package:
Here's my code:
import 'package:shared_preferences/shared_preferences.dart';
class SharedPreferencesProvider {
SharedPreferences prefs;
SharedPreferencesProvider() {
init();
}
// Get get instance of SharedPreferences upon instantiation.
void init() async {
prefs = await SharedPreferences.getInstance();
}
// Save some data
void saveData() {
prefs.setString('key', 'value');
}
}
When I run this, I get an error stating that setString was called on null. I did some investigating, and it appears that the prefs property is never actually initiated. It really is null when called by saveData().
I'm guessing this has to do with async operations in Dart and the way constructors work, but I'm not sure how to get around it. How can I initialize my prefs property so I can utilize it across my class?