Prefs constructor

const Prefs()

Annotation to define values to be handled in Shared Preferences and to enable type-safe storage and retrieval.

Define the class as follows and use build_runner for code generation.

Shared Preferencesで取り扱う値を定義しタイプセーフに保存・取得を可能にするためのアノテーション。


class PrefsValue with _$PrefsValue, ChangeNotifier {
  factory PrefsValue({
    String? userToken,
    @Default(1.0) double valumeSetting,
  }) = _PrefsValue;

You can create an object with initial values based on the generated class.


final prefs = PrefsValue(
  userToken: "abcdefg...",
  valumeSetting: 0.5,

The generated objects have the methods get() and set() available for each value. Execution of each method allows the value to be retrieved and stored.

It is also possible to monitor value updates when each value's set() is executed by addListener on the generated object itself.



final userToken = prefs.userToken.get();
await prefs.userToken.set("new user token");


const Prefs();