Skip to content

Instantly share code, notes, and snippets.

@bizz84
Created August 2, 2024 09:06
Show Gist options
  • Save bizz84/f6bbcb69cc1f235b28f7a297363650bc to your computer and use it in GitHub Desktop.
Save bizz84/f6bbcb69cc1f235b28f7a297363650bc to your computer and use it in GitHub Desktop.
Example showing that Future.ignore() doesn't work as expected
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