Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GIfatahTH/fb8149542bfc3bb7b1de4cc58c47d737 to your computer and use it in GitHub Desktop.
Save GIfatahTH/fb8149542bfc3bb7b1de4cc58c47d737 to your computer and use it in GitHub Desktop.
class FutureCounterWithError extends ChangeNotifier {
int _count = 0;
int get count => _count;
bool isLoaded = false;
bool hasError = false;
String errorMessage;
Future<void> increment() async {
isLoaded = false;
notifyListeners();
await Future<void>.delayed(const Duration(seconds: 1));
try {
if (Random().nextBool()) {
throw CounterError('There is a problem in your counter');
}
isLoaded = true;
hasError = false;
_count++;
notifyListeners();
} catch (e) {
hasError = true;
errorMessage = 'There is a problem in your counter';
notifyListeners();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment