load method

Future<T?> load()

Load state from storage

Implementation

Future<T?> load() async {
  try {
    _printDebug('Starting load...');

    _printDebug('Loading from storage');

    // Load from storage
    Uint8List? data;
    try {
      data = await storage.load();
    } catch (error) {
      throw StorageException('On load: ${error.toString()}');
    }

    _printDebug('Running load raw transformations');

    try {
      // Run all raw load transforms
      rawTransforms?.onLoad?.forEach((transform) {
        data = transform(data);
      });
    } catch (error) {
      throw TransformationException(
        'On load raw transformation: ${error.toString()}',
      );
    }

    _printDebug('Deserializing');

    T? state;
    try {
      state = serializer.decode(data);
    } catch (error) {
      throw SerializationException('On load: ${error.toString()}');
    }

    _printDebug('Running load transformations');

    try {
      // Run all load transforms
      transforms?.onLoad?.forEach((transform) {
        state = transform(state);
      });
    } catch (error) {
      throw TransformationException(
        'On load transformation: ${error.toString()}',
      );
    }

    _printDebug('Done loading!');

    return state;
  } catch (error) {
    _printDebug('Error while loading: ${error.toString()}');
    rethrow;
  }
}