restore method

  1. @override
Future<void> restore()
inherited

Implementation

@override
Future<void> restore() async {
  // If we haven't stored our version yet, we need to store it and stop
  if (!storage.has(context)) {
    // print('Storing $context');
    await storage.set(context, {'version': version});
    await storage.set(storageKey, <String, dynamic>{});
    return;
  }

  // If we have stored our version, but it doesn't match, we need to delete the previous data,
  // create a new version, and stop
  final String storedVersion = storage.get(context)['version'];
  if (storedVersion != version) {
    // print('Updating storage from $storedVersion to $version');
    await storage.delete('$storedVersion//$context');
    await storage.set(context, {'version': version});
    await storage.set(storageKey, <String, dynamic>{});
    return;
  }

  if (storage.has(storageKey)) {
    // If there is invalid data, delete the stored data
    // print('Restoring $storageKey');
    try {
      for (var entry in storage.get(storageKey).entries) {
        // print(entry);
        data[entry.key] = fromJson(entry.value);
      }
    } catch (e) {
      // print('Error restoring $storageKey: $e');
      await storage.delete(storedVersion);
      rethrow;
    }
  }
}