loginModel static method

dynamic loginModel(
  1. String key,
  2. Model toModel(
    1. dynamic data
    )
)

Login a model into the app. key that the user was stored under. toModel is a function that converts the data into a model. the data parameter will contain the data that was stored in the storage. Example:

await Auth.loginModel('my_auth_key', (data) => Customer.fromJson(data));

Implementation

static loginModel(String key, Model Function(dynamic data) toModel) async {
  dynamic object = await NyStorage.read(key);
  if (object == null) return;

  if (object is String) {
    try {
      object = object.parseJson();
    } on Exception catch (e) {
      dump(e.toString());
      return;
    }
  }
  Model model = toModel(object);

  Backpack.instance.set(key, model);
}