roughike/streaming_shared_preferences

ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.

timosturm opened this issue · 2 comments

This error occurs when running the example app:

Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk...
E/flutter (18643): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter (18643): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter (18643): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter (18643): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter (18643): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter (18643): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter (18643): #3      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:146:35)
E/flutter (18643): #4      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
E/flutter (18643): #5      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:356:48)
E/flutter (18643): #6      MethodChannelSharedPreferencesStore.getAll (package:shared_preferences_platform_interface/method_channel_shared_preferences.dart:54:22)
E/flutter (18643): #7      SharedPreferences._getSharedPreferencesMap (package:shared_preferences/shared_preferences.dart:187:57)
E/flutter (18643): #8      SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart:54:19)
E/flutter (18643): #9      debugObtainSharedPreferencesInstance (package:streaming_shared_preferences/src/streaming_shared_preferences.dart:300:23)
E/flutter (18643): #10     debugObtainSharedPreferencesInstance (package:streaming_shared_preferences/src/streaming_shared_preferences.dart:299:27)
E/flutter (18643): #11     StreamingSharedPreferences.instance (package:streaming_shared_preferences/src/streaming_shared_preferences.dart:40:7)
E/flutter (18643): #12     main (package:privacyidea_authenticator/testPref.dart:25:56)
E/flutter (18643): #13     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:241:25)
E/flutter (18643): #14     _rootRun (dart:async/zone.dart:1184:13)
E/flutter (18643): #15     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter (18643): #16     _runZoned (dart:async/zone.dart:1619:10)
E/flutter (18643): #17     runZonedGuarded (dart:async/zone.dart:1608:12)
E/flutter (18643): #18     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:233:5)
E/flutter (18643): #19     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (18643): #20     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

To prevent this, the main method should be changed to:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  /// Obtain instance to streaming shared preferences, create MyAppSettings, and
  /// once that's done, run the app.
  final preferences = await StreamingSharedPreferences.instance;
  final settings = MyAppSettings(preferences);

  runApp(MyApp(settings));
}

This should be mentioned in the documentation of this plugin also.

Just hit the exact same problem. Will try to do an update and Pull Request.

Fixed by #10.