Search code examples
fluttersharedpreferencesflutter-dependencies

How can I add item to SharedPreferences with out coding in Flutter?


I am new on flutter. I'm using the SharedPreferences to keeping tokens and etc.

It works like below.

final prefs = await SharedPreferences.getInstance();
String token = data['token'].toString();
prefs.setString("token", token);

Also, I would like to store my API user and API password in SharedPreferences. This information never gonna change. But I don't want to add user/password information with prefs.setString("userkey", "uservalue"); I would like to add them with some kind of configuration or something like that. Not with the hard coding.

How can I do that?


Solution

  • The best practice is to store it in a file constants.dart

    class Constants {
      static const String _kUser = " USERNAME";
      static const String _kKey = " APIKEY";
    }