0

I have a simple question regering the shared_preferences package in flutter.

Is it safe to get an instance of SharedPreferences and store that for future use or is it best to always get a new reference each time?

To clarify, which of these two are the proper or best way of doing it?

SharedPreferences preferences;

void init() async {
  preferences = await SharedPreferences.getInstance();
}

void setValue(String key, String value)  async{
  await preferences.setString(key, value);
}
void setValue(String key, String value) async {
  final preferences = await SharedPreferences.getInstance();
  await preferences.setString(key, value);
}
3
  • It's safe to do it either way. May 29, 2020 at 1:31
  • Even after bringing the app back from the background?
    – Isak
    May 29, 2020 at 4:42
  • Yes that doesn't make a difference. May 29, 2020 at 5:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.