Skip to content

Commit dc1cca5

Browse files
committed
try to ensure only one pop occurs
1 parent 8ae4f2e commit dc1cca5

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

lib/pages/signing/sub_widgets/sign_message_tab.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class _SignMessageFormState extends ConsumerState<SignMessageForm> {
9090

9191
messageController.text = ref.read(_pSignState).message;
9292

93-
_chooseAddress = IfNotAlreadyAsync(() async {
93+
_chooseAddress = IfNotAlreadyAsync<void>(() async {
9494
final Address? address;
9595

9696
if (Util.isDesktop) {
@@ -166,7 +166,7 @@ class _SignMessageFormState extends ConsumerState<SignMessageForm> {
166166
}
167167
}).execute;
168168

169-
_sign = IfNotAlreadyAsync(() async {
169+
_sign = IfNotAlreadyAsync<void>(() async {
170170
Exception? ex;
171171

172172
final state = ref.read(_pSignState);

lib/pages/signing/sub_widgets/verify_message_tab.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class _VerifyMessageFormState extends ConsumerState<VerifyMessageForm> {
8282
messageController.text = ref.read(_pVerifyState).message;
8383
signatureController.text = ref.read(_pVerifyState).signature;
8484

85-
_verify = IfNotAlreadyAsync(() async {
85+
_verify = IfNotAlreadyAsync<void>(() async {
8686
Exception? ex;
8787

8888
final verified = await showLoading(

lib/utilities/if_not_already.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@ class IfNotAlready {
1818
}
1919
}
2020

21-
class IfNotAlreadyAsync {
22-
final Future<void> Function() _function;
21+
class IfNotAlreadyAsync<T> {
22+
final Future<void> Function()? _function;
23+
final Future<void> Function(T? args)? _functionWithArgs;
2324

2425
bool _locked = false;
2526

26-
IfNotAlreadyAsync(this._function);
27+
IfNotAlreadyAsync(this._function) : _functionWithArgs = null;
28+
IfNotAlreadyAsync.withArgs(this._functionWithArgs) : _function = null;
2729

28-
Future<void> execute() async {
30+
Future<void> execute([T? args]) async {
2931
if (!_locked) {
3032
_locked = true;
3133
try {
32-
await _function();
34+
if (_function == null) {
35+
await _function!();
36+
} else {
37+
await _functionWithArgs!(args);
38+
}
3339
} finally {
3440
_locked = false;
3541
}

lib/widgets/qr_scanner.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
55
import 'package:qr_code_scanner_plus/qr_code_scanner_plus.dart';
66

77
import '../themes/stack_colors.dart';
8+
import '../utilities/if_not_already.dart';
89
import '../utilities/text_styles.dart';
910
import 'background.dart';
1011
import 'custom_buttons/app_bar_icon_button.dart';
@@ -23,10 +24,18 @@ class _QrScannerState extends State<QrScanner> {
2324

2425
StreamSubscription<Barcode>? sub;
2526

26-
void _onScanned(String? data) {
27-
if (data != null && mounted) {
28-
Navigator.of(context).pop(data);
29-
}
27+
late final Future<void> Function(String?) _onScanned;
28+
29+
@override
30+
void initState() {
31+
super.initState();
32+
33+
_onScanned = IfNotAlreadyAsync<String>.withArgs((data) async {
34+
await sub?.cancel();
35+
if (mounted) {
36+
Navigator.of(context).pop(data);
37+
}
38+
}).execute;
3039
}
3140

3241
// In order to get hot reload to work we need to pause the camera if the platform

0 commit comments

Comments
 (0)