Skip to content

Commit 9e7f33a

Browse files
committed
Handle no internet connection & timeout
1 parent 215b4f1 commit 9e7f33a

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

app/lib/screens/wallets/contracts.dart

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_riverpod/flutter_riverpod.dart';
35
import 'package:threebotlogin/helpers/logger.dart';
@@ -7,6 +9,7 @@ import 'package:threebotlogin/services/tfchain_service.dart';
79
import 'package:gridproxy_client/models/contracts.dart';
810
import 'package:threebotlogin/widgets/wallets/contract_details.dart';
911
import 'package:threebotlogin/helpers/contract_helpers.dart';
12+
import 'package:connectivity_plus/connectivity_plus.dart';
1013

1114
class WalletContractsWidget extends ConsumerStatefulWidget {
1215
const WalletContractsWidget({super.key, required this.wallet});
@@ -33,35 +36,74 @@ class _WalletContractsWidgetState extends ConsumerState<WalletContractsWidget> {
3336
loading = true;
3437
failed = false;
3538
});
39+
3640
try {
37-
final twinId = await getTwinId(widget.wallet.tfchainSecret);
38-
contracts = await getContractsByTwinId(twinId);
39-
} catch (e) {
40-
logger.e('Failed to load contracts: $e');
41-
setState(() {
42-
failed = true;
43-
});
44-
if (context.mounted) {
45-
final loadingContractsFailure = SnackBar(
46-
content: Text(
47-
'Failed to load contracts',
48-
style: Theme.of(context)
49-
.textTheme
50-
.bodyMedium!
51-
.copyWith(color: Theme.of(context).colorScheme.errorContainer),
52-
),
53-
duration: const Duration(seconds: 3),
41+
final connectivityResult = await (Connectivity().checkConnectivity());
42+
43+
if (connectivityResult.contains(ConnectivityResult.none)) {
44+
_handleFailure(
45+
'No internet connection. Please check your network.',
5446
);
55-
ScaffoldMessenger.of(context).clearSnackBars();
56-
ScaffoldMessenger.of(context).showSnackBar(loadingContractsFailure);
47+
return;
5748
}
58-
} finally {
49+
50+
final twinId = await getTwinId(widget.wallet.tfchainSecret).timeout(
51+
const Duration(minutes: 1),
52+
onTimeout: () {
53+
throw TimeoutException('Loading contracts timed out');
54+
},
55+
);
56+
57+
contracts = await getContractsByTwinId(twinId).timeout(
58+
const Duration(minutes: 1),
59+
onTimeout: () {
60+
throw TimeoutException('Loading contracts timed out');
61+
},
62+
);
63+
5964
setState(() {
6065
loading = false;
66+
failed = false;
6167
});
68+
} on TimeoutException catch (e) {
69+
_handleFailure(
70+
'Loading contracts timed out. Please check your network.',
71+
error: e,
72+
);
73+
} catch (e) {
74+
_handleFailure(
75+
'Failed to load contracts. Please try again.',
76+
error: e,
77+
);
6278
}
6379
}
6480

81+
void _handleFailure(String userMessage, {Object? error}) {
82+
if (error != null) {
83+
logger.e('Load contracts failed', error: error);
84+
}
85+
86+
if (mounted) {
87+
final errorSnackbar = SnackBar(
88+
content: Text(
89+
userMessage,
90+
style: Theme.of(context)
91+
.textTheme
92+
.bodyMedium!
93+
.copyWith(color: Theme.of(context).colorScheme.errorContainer),
94+
),
95+
duration: const Duration(seconds: 3),
96+
);
97+
ScaffoldMessenger.of(context).clearSnackBars();
98+
ScaffoldMessenger.of(context).showSnackBar(errorSnackbar);
99+
}
100+
101+
setState(() {
102+
loading = false;
103+
failed = true;
104+
});
105+
}
106+
65107
@override
66108
Widget build(BuildContext context) {
67109
if (loading) {

0 commit comments

Comments
 (0)