From 41af6b716e40a1410419c9d1258512b16efbaa7e Mon Sep 17 00:00:00 2001 From: rby d Date: Sat, 8 Jul 2023 13:29:44 +0200 Subject: [PATCH] Fix initializing binding outside the app zone --- lib/core/misc/error_handler.dart | 23 ++++++++--------------- lib/main.dart | 31 ++++++++++++++++--------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/lib/core/misc/error_handler.dart b/lib/core/misc/error_handler.dart index 95925c520..1602ec725 100644 --- a/lib/core/misc/error_handler.dart +++ b/lib/core/misc/error_handler.dart @@ -23,8 +23,7 @@ import 'package:shared_preferences/shared_preferences.dart'; /// information about error reporting in Flutter. class ErrorHandler with LoggerMixin { ErrorHandler({ - required Widget child, - required SharedPreferences sharedPreferences, + required Future Function() builder, }) { if (kReleaseMode) { // override the error widget in release mode (the red error screen) @@ -35,18 +34,16 @@ class ErrorHandler with LoggerMixin { runZonedGuarded( () async { - if (kReleaseMode && sentryDns.isNotEmpty) { - await SentryFlutter.init( - (options) => options.dsn = sentryDns, - ).handleError(logErrorHandler); - } - - runApp(child); + WidgetsFlutterBinding.ensureInitialized(); + sharedPreferences = await SharedPreferences.getInstance(); + runApp(await builder()); }, - (e, st) => _handleError(e, st, sharedPreferences: sharedPreferences), + _handleError, ); } + late final SharedPreferences sharedPreferences; + /// Handles errors caught by the Flutter framework. /// /// Forwards the error to the [_handleError] handler when in release mode. @@ -68,11 +65,7 @@ class ErrorHandler with LoggerMixin { } /// Prints the error and reports it to sentry in release mode. - Future _handleError( - Object error, - StackTrace stackTrace, { - required SharedPreferences sharedPreferences, - }) async { + Future _handleError(Object error, StackTrace stackTrace) async { if (error is SocketException) { // no internet connection, can be ignored log.warning( diff --git a/lib/main.dart b/lib/main.dart index 3a610b51e..3a9042ca7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -16,23 +16,24 @@ import 'package:shared_preferences/shared_preferences.dart'; /// `--dart-define=twitter_consumer_key=your_consumer_key` and /// `--dart-define=twitter_consumer_secret=your_consumer_secret`. Future main() async { - WidgetsFlutterBinding.ensureInitialized(); - final sharedPreferences = await SharedPreferences.getInstance(); - - // pre-cache flare animations - FlareCache.doesPrune = false; - await warmupFlare(); - // ErrorHandler will run the app and handle uncaught errors ErrorHandler( - sharedPreferences: sharedPreferences, - child: ProviderScope( - overrides: [ - sharedPreferencesProvider.overrideWithValue(sharedPreferences), - ], - observers: [ProviderLogger()], - child: const HarpyApp(), - ), + builder: () async { + WidgetsFlutterBinding.ensureInitialized(); + final sharedPreferences = await SharedPreferences.getInstance(); + + // pre-cache flare animations + FlareCache.doesPrune = false; + await warmupFlare(); + + return ProviderScope( + overrides: [ + sharedPreferencesProvider.overrideWithValue(sharedPreferences), + ], + observers: [ProviderLogger()], + child: const HarpyApp(), + ); + }, ); }