-
Notifications
You must be signed in to change notification settings - Fork 28.6k
Restore error message explaining the need to call WidgetsFlutterBinding.ensureInitialized() #78028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you run SharedPreferences.getInstance() Before runApp()? |
Hi @lukepighetti When running your code sample on the logs[ +213 ms] Connecting to service protocol: http://127.0.0.1:62225/MZ5j6RDbyEE=/
[ +242 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at
http://127.0.0.1:62225/MZ5j6RDbyEE=/.
[ +130 ms] DDS is listening at http://127.0.0.1:62233/df3nJKVqXD4=/.
[ +69 ms] [VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before
the binding was initialized.
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.
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.
#0 defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:97:7)
#1 defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:110:4)
#2 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:62)
#3 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
#4 MethodChannel.invokeMethod (package:flutter/src/services/platfor<…>
[ +41 ms] Successfully connected to service protocol: http://127.0.0.1:62225/MZ5j6RDbyEE=/
[+1604 ms] DevFS: Creating new filesystem on the device (null)
[ +20 ms] DevFS: Created new filesystem on the device
(file:///Users/tahatesser/Library/Developer/CoreSimulator/Devices/BD94A45F-A994-4FC3-BAD9-448D8D2E0F1B/data/Containers/Data/Ap
plication/ED2609BF-51B0-4EF4-BD19-074669020D42/tmp/stable_flutter9BnASB/stable_flutter/)
[ +9 ms] Updating assets
[ +168 ms] Syncing files to device iPhone 12... However on the logs[ +215 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at
http://127.0.0.1:61914/nvKr2mRoZAY=/.
[ +169 ms] DDS is listening at http://127.0.0.1:61920/3Ju2HOPbk8o=/.
[ +84 ms] [VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: Null check operator used on a null value
#0 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
#2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
#3 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:358:49)
#4 MethodChannelSharedPreferencesStore.getAll
(package:shared_preferences_platform_interface/method_channel_shared_preferences.dart:44:25)
#5 SharedPreferences._getSharedPreferencesMap (package:shared_preferences/shared_preferences.dart:181:57)
#6 SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart:57:19)
#7 main (package:triage/main.dart:5:41)
#8 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:146:25)
#9 _rootRun (dart:async/zone.dart:1354:1<…>
[ +40 ms] Successfully connected to service protocol: http://127.0.0.1:61914/nvKr2mRoZAY=/
[ +100 ms] DevFS: Creating new filesystem on the device (null)
[ +31 ms] DevFS: Created new filesystem on the device
(file:///Users/tahatesser/Library/Developer/CoreSimulator/Devices/BD94A45F-A994-4FC3-BAD9-448D8D2E0F1B/data/Containers/Data/Ap
plication/EAD05D9E-EAFA-441E-84A6-102FE2F98379/tmp/dev_flutterePbWK0/dev_flutter/)
[ +6 ms] Updating assets
[ +195 ms] Syncing files to device iPhone 12... code sampleimport 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: Text('shared_preferences error'),
),
),
);
}
}
flutter doctor -v[✓] Flutter (Channel stable, 2.0.1, on macOS 11.2.3 20D91 darwin-x64, locale en-GB)
• Flutter version 2.0.1 at /Users/tahatesser/Code/flutter_stable
• Framework revision c5a4b4029c (8 days ago), 2021-03-04 09:47:48 -0800
• Engine revision 40441def69
• Dart version 2.12.0
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Volumes/Extreme/SDK
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /Volumes/Extreme/SDK
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer
• Xcode 12.4, Build version 12D4e
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[✓] VS Code (version 1.54.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.20.0
[✓] Connected device (3 available)
• iPhone 12 (mobile) • BD94A45F-A994-4FC3-BAD9-448D8D2E0F1B • ios •
com.apple.CoreSimulator.SimRuntime.iOS-14-4 (simulator)
• macOS (desktop) • macos • darwin-x64 • macOS
11.2.3 20D91 darwin-x64
• Chrome (web) • chrome • web-javascript • Google
Chrome 89.0.4389.82
• No issues found! [✓] Flutter (Channel beta, 2.0.1, on macOS 11.2.3 20D91 darwin-x64, locale en-GB)
• Flutter version 2.0.1 at /Users/tahatesser/Code/flutter_beta
• Framework revision c5a4b4029c (8 days ago), 2021-03-04 09:47:48 -0800
• Engine revision 40441def69
• Dart version 2.12.0
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Volumes/Extreme/SDK
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /Volumes/Extreme/SDK
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer
• Xcode 12.4, Build version 12D4e
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[✓] VS Code (version 1.54.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.20.0
[✓] Connected device (3 available)
• iPhone 12 (mobile) • BD94A45F-A994-4FC3-BAD9-448D8D2E0F1B • ios •
com.apple.CoreSimulator.SimRuntime.iOS-14-4 (simulator)
• macOS (desktop) • macos • darwin-x64 • macOS
11.2.3 20D91 darwin-x64
• Chrome (web) • chrome • web-javascript • Google
Chrome 89.0.4389.82
• No issues found! [✓] Flutter (Channel dev, 2.1.0-10.0.pre, on macOS 11.2.3 20D91 darwin-x64, locale en-GB)
• Flutter version 2.1.0-10.0.pre at /Users/tahatesser/Code/flutter_dev
• Framework revision cc9b78fc5c (2 weeks ago), 2021-02-25 13:26:03 -0800
• Engine revision a252ec09b7
• Dart version 2.13.0 (build 2.13.0-77.0.dev)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Volumes/Extreme/SDK
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /Volumes/Extreme/SDK
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer
• Xcode 12.4, Build version 12D4e
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[✓] VS Code (version 1.54.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.20.0
[✓] Connected device (3 available)
• iPhone 12 (mobile) • BD94A45F-A994-4FC3-BAD9-448D8D2E0F1B • ios •
com.apple.CoreSimulator.SimRuntime.iOS-14-4 (simulator)
• macOS (desktop) • macos • darwin-x64 • macOS
11.2.3 20D91 darwin-x64
• Chrome (web) • chrome • web-javascript • Google
Chrome 89.0.4389.82
• No issues found! [✓] Flutter (Channel master, 2.1.0-11.0.pre.229, on macOS 11.2.3 20D91 darwin-x64, locale
en-GB)
• Flutter version 2.1.0-11.0.pre.229 at /Users/tahatesser/Code/flutter_master
• Framework revision d20ec4c7d8 (9 hours ago), 2021-03-11 22:18:51 -0800
• Engine revision 1b68503bc8
• Dart version 2.13.0 (build 2.13.0-125.0.dev)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Volumes/Extreme/SDK
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /Volumes/Extreme/SDK
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer
• Xcode 12.4, Build version 12D4e
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[✓] VS Code (version 1.54.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.20.0
[✓] Connected device (4 available)
• Redmi K20 Pro (mobile) • def0ad20 • android-arm64 •
Android 10 (API 29)
• iPhone 12 (mobile) • BD94A45F-A994-4FC3-BAD9-448D8D2E0F1B • ios •
com.apple.CoreSimulator.SimRuntime.iOS-14-4 (simulator)
• macOS (desktop) • macos • darwin-x64 •
macOS 11.2.3 20D91 darwin-x64
• Chrome (web) • chrome • web-javascript •
Google Chrome 89.0.4389.82
• No issues found! Thank you for filing the issue! |
I almost feel comfortable closing this issue now that it's documented. I'll leave it up to you. Thanks for the fast triage. |
I'm already working on that. |
@Hixie Is restoring this error message still something that's in progress? |
@Hixie @stuartmorgan It looks like this is already in progress, but if it is not, I am happy to take a shot at it. Putting the assertion back in place seems simple enough. It looks like you are planning to resolve this as part of a much larger change though. Edit: It looks like a basic error message was already added back in master at least for MethodChannel in #92758. Looks like the in progress PR adds some more context and possibly some utils for checking that the bindings have been initialized. I'll wait for that 👍 |
Yeah a comprehensive fix is in #89451. It's taking a long time to land because it requires an intense number of changes to Google's codebase (Google has contributed all its code as tests, which is fantastic in terms of catching regressions, but makes life non-trivial when changing a core API...). (For non-Google codebases, |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
When using
shared_preferences: ^2.0.4
, when callingSharedPreferences.getInstance()
I get a null check exception.Fails on
Succeeds on
Flutter version & devices
MCVE
The text was updated successfully, but these errors were encountered: