I am trying to generate through injectable, I done flutter clean also it won't work, I am using stable version flutter 2.2.2, I am facing null check error on this line of code
this code is throwing error
SharedPreferences.getInstance();
also this code
@module
abstract class ThemeModule {
@preResolve
Future<SharedPreferences> get prefs async =>
await SharedPreferences.getInstance();
}
E/flutter (27063): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
E/flutter (27063): #0 MethodChannel.binaryMessenger
package:flutter/…/services/platform_channel.dart:142
E/flutter (27063): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:148
E/flutter (27063): #2 MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:331
E/flutter (27063): #3 MethodChannel.invokeMapMethod
package:flutter/…/services/platform_channel.dart:358
E/flutter (27063): #4 MethodChannelSharedPreferencesStore.getAll
package:shared_preferences_platform_interface/method_channel_shared_preferences.dart:44
E/flutter (27063): #5 SharedPreferences._getSharedPreferencesMap
package:shared_preferences/shared_preferences.dart:180
E/flutter (27063): #6 SharedPreferences.getInstance
package:shared_preferences/shared_preferences.dart:56
E/flutter (27063): #7 main
package:financial/main.dart:12
E/flutter (27063): #8 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:142:25)
E/flutter (27063): #9 _rootRun (dart:async/zone.dart:1354:13)
E/flutter (27063): #10 _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (27063): #11 _runZoned (dart:async/zone.dart:1789:10)
E/flutter (27063): #12 runZonedGuarded (dart:async/zone.dart:1777:12)
E/flutter (27063): #13 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:138:5)
E/flutter (27063): #14 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (27063): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
Use the fallback operator to set a default value for null values before using the variable. Here, "str" is null, and we set the fallback operator with fallback value in case of "str" is null. You need to do this before using it on the code.
Coding example for the question ERROR:flutter/lib/ui/ui_dart_state.cc(199) Unhandled Exception: Null check operator used on a null value-Flutter Home Services
Another solution is to check the "mounted" property of this object before calling setState () to ensure the object is still in the tree. [ ] E/flutter ( 5984): This error might indicate a memory leak if setState () is being called because another object is retaining a reference to this State object after it has been removed from the tree.
[ ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update. [ +1 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update. [ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update. [ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
If using the gradle wrapper, try editing the distributionUrl in /Users/haoranliu/StudioProjects/flutter_tim_tuikit/flutter_tim_tuikit/android/gradle/wrapper/gradle-wrapper.properties to gradle-5.4.1-all.zip * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
I have the same issue and the solution is adding WidgetsFlutterBinding.ensureInitialized()
in main function before run app:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// init your dependency injection here
runApp(MyApp());}
From the docs:
Returns an instance of the WidgetsBinding, creating and initializing it if necessary. If one is created, it will be a WidgetsFlutterBinding. If one was previously initialized, then it will at least implement WidgetsBinding.
You only need to call this method if you need the binding to be initialized before calling runApp.
I also ran into that problem. However, the answer from @badasz was not a solution, because the exception disussed in the github issue was a completely different one. In my case, i ran the codeSharedPreferences.getInstance()
before i called runApp(Widget app)
. That led to the given error.
Instead of this
void main() {
// qrCode.make();
SharedPreferences.getInstance();
Firebase.initializeApp();
// FirebaseApp defaultApp = Firebase.app();
runApp(const MyApp());
}
Add like this
You must add WidgetsFlutterBinding.ensureInitialized();
starting of main method
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// qrCode.make();
FirebaseApp app = await Firebase.initializeApp();
// FirebaseApp defaultApp = Firebase.app();
runApp(const MyApp());
}
Reason: This library depends on the native library(android ,ios etc platofrm). Make sure that you have an instance of the WidgetsBinding, which is required to use platform channels to call the native code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With