Skip to content

Commit 69f564f

Browse files
committed
fix: Fixed some UnmountedRefException occurring with backups.
1 parent b33a85d commit 69f564f

1 file changed

Lines changed: 46 additions & 22 deletions

File tree

lib/model/backup.dart

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io';
44

55
import 'package:flutter/foundation.dart';
66
import 'package:flutter_riverpod/flutter_riverpod.dart';
7+
import 'package:flutter_riverpod/misc.dart';
78
import 'package:open_authenticator/app.dart';
89
import 'package:open_authenticator/i18n/localizable_exception.dart';
910
import 'package:open_authenticator/i18n/translations.g.dart';
@@ -29,34 +30,57 @@ class BackupStore extends AsyncNotifier<List<Backup>> {
2930

3031
/// Imports the [backupFile].
3132
Future<Result<Backup>> import(File backupFile) async {
32-
if (!Backup.isValidBackup(backupFile)) {
33-
return ResultError(exception: _InvalidBackupContentException());
34-
}
35-
DateTime? dateTime = _fromBackupFilename(backupFile);
36-
Backup backup = Backup._(ref: ref, dateTime: dateTime ?? DateTime.now());
37-
List<Backup> backups = [...(await future), backup]..sort();
38-
Directory directory = await getBackupsDirectory(create: true);
39-
backupFile.copySync(join(directory.path, backup.filename));
40-
if (!ref.mounted) {
41-
return const ResultCancelled();
33+
KeepAliveLink keepAliveLink = ref.keepAlive();
34+
try {
35+
if (!Backup.isValidBackup(backupFile)) {
36+
return ResultError(exception: _InvalidBackupContentException());
37+
}
38+
DateTime? dateTime = _fromBackupFilename(backupFile);
39+
Backup backup = Backup._(ref: ref, dateTime: dateTime ?? DateTime.now());
40+
List<Backup> backups = [...(await future), backup]..sort();
41+
Directory directory = await getBackupsDirectory(create: true);
42+
backupFile.copySync(join(directory.path, backup.filename));
43+
if (!ref.mounted) {
44+
return const ResultCancelled();
45+
}
46+
state = AsyncData(backups);
47+
return ResultSuccess(value: backup);
48+
} catch (ex, stackTrace) {
49+
return ResultError(
50+
exception: ex,
51+
stackTrace: stackTrace,
52+
);
53+
} finally {
54+
keepAliveLink.close();
4255
}
43-
state = AsyncData(backups);
44-
return ResultSuccess(value: backup);
4556
}
4657

4758
/// Do a backup with the given password.
4859
Future<Result<Backup>> doBackup(String password) async {
49-
Backup backup = Backup._(ref: ref, dateTime: DateTime.now());
50-
Result result = await backup.save(password);
51-
if (result is! ResultSuccess) {
52-
return result.to<Backup>((value) => null);
53-
}
54-
List<Backup> backups = [...(await future), backup]..sort();
55-
if (!ref.mounted) {
56-
return const ResultCancelled();
60+
KeepAliveLink keepAliveLink = ref.keepAlive();
61+
try {
62+
Backup backup = Backup._(ref: ref, dateTime: DateTime.now());
63+
Result result = await backup.save(password);
64+
if (result is! ResultSuccess) {
65+
return result.to<Backup>((value) => null);
66+
}
67+
if (!ref.mounted) {
68+
return const ResultCancelled();
69+
}
70+
List<Backup> backups = [...(await future), backup]..sort();
71+
if (!ref.mounted) {
72+
return const ResultCancelled();
73+
}
74+
state = AsyncData(backups);
75+
return ResultSuccess(value: backup);
76+
} catch (ex, stackTrace) {
77+
return ResultError(
78+
exception: ex,
79+
stackTrace: stackTrace,
80+
);
81+
} finally {
82+
keepAliveLink.close();
5783
}
58-
state = AsyncData(backups);
59-
return ResultSuccess(value: backup);
6084
}
6185

6286
/// Lists available backups.

0 commit comments

Comments
 (0)