Created
August 2, 2024 09:06
-
-
Save bizz84/f6bbcb69cc1f235b28f7a297363650bc to your computer and use it in GitHub Desktop.
Example showing that Future.ignore() doesn't work as expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:developer'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
FlutterError.onError = (details) { | |
log('FlutterError error: ${details.exception}'); | |
FlutterError.presentError(details); | |
}; | |
PlatformDispatcher.instance.onError = (error, stack) { | |
log('PlatformDispatcher error: $error'); | |
return true; | |
}; | |
runApp(const MainApp()); | |
} | |
Future<void> someFuture() => throw UnimplementedError(); | |
class ButtonThatThrows extends StatelessWidget { | |
const ButtonThatThrows({super.key}); | |
@override | |
Widget build(BuildContext context) => ElevatedButton( | |
onPressed: () async { | |
log('before'); | |
someFuture().ignore(); | |
log('after'); | |
}, | |
child: const Text('Throw')); | |
} | |
class MainApp extends StatelessWidget { | |
const MainApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: ButtonThatThrows(), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment