json<T> static method

Prf<T> json<T>(
  1. String key, {
  2. required T fromJson(
    1. Map<String, dynamic> json
    ),
  3. required Map<String, dynamic> toJson(
    1. T object
    ),
  4. T? defaultValue,
})

Creates a new preference for a JSON-serializable object.

This factory method sets up a Prf instance with a JsonAdapter for converting between the object and its JSON representation.

  • key is the key to store the preference under.
  • fromJson converts a JSON map to an instance of type T.
  • toJson converts an instance of type T to a JSON map.
  • defaultValue is the value to use if no value exists for the key.

Implementation

static Prf<T> json<T>(
  String key, {
  required T Function(Map<String, dynamic> json) fromJson,
  required Map<String, dynamic> Function(T object) toJson,
  T? defaultValue,
}) {
  return Prf._withAdapter(
    key,
    adapter: JsonAdapter<T>(fromJson: fromJson, toJson: toJson),
    defaultValue: defaultValue,
  );
}