'How to fix this nullOk error when using the flutter_svg package?
I connected the package, added it to main.dart, I try to compile the application, but I get this error. Help! What should I do about it?
error
Launching lib\main.dart on XT1562 in debug mode...
Running Gradle task 'assembleDebug'...
/D:/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_svg-0.18.1/lib/src/picture_provider.dart:50:59: Error: No named parameter with the name 'nullOk'.
context != null ? Localizations.localeOf(context, nullOk: true) : null,
^^^^^^
/D:/flutter/packages/flutter/lib/src/widgets/localizations.dart:413:17: Context: Found this candidate, but the arguments don't match.
static Locale localeOf(BuildContext context) {
^^^^^^^^
FAILURE: Build failed with an exception.
* Where:
Script 'D:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 991
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'D:\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 15s
Exception: Gradle task assembleDebug failed with exit code 1
Solution 1:[1]
Follow the steps below
- add the following dependency
flutter_svg: ^0.20.0-nullsafety.3
- run
flutter cleancommand - run
flutter pub getcommand - run
flutter runcommand or run the project
Solution 2:[2]
Everyone is right: putting flutter_svg: ^0.20.0-nullsafety.3 or flutter_svg: ^0.19.3 (in case you can't migrate to null safety yet) is the solution.
I want to add a little thing: if you're using a package which depends on flutter_svg and you still get the error due to older versions usage, you can fix it in this way:
dependency_overrides:
flutter_svg: ^0.19.3
I lost plenty of time because the flags: ^3.2.2 package was using flutter_svg: ^0.19.1 and it led to the same error.
Solution 3:[3]
The problem is that the version of Flutter with null-safety enabled (currently on the beta channel as of writing), the nullOk parameter was removed Localizations.localeOf, but you're using a package (in this case, flutter_svg) that is still using nullOk.
If you're using the null-safe version of Flutter, you should use null-safe versions of other packages too, if available. Alternatively you can stick with the latest version of Flutter without null-safety (version 1.22.x) until all of your dependent packages have been migrated for null-safety.
Solution 4:[4]
I have faced the same problem . After I finally got the solution. Here Simple:
At first you add Pre-Release the following dependency:
flutter_svg: ^0.20.0-nullsafety.3
Then run flutter pub get command
Now you can run your project . See that there is no error .
Solution 5:[5]
On line 57 of the locally cached file /lib/src/picture_provider.dart (see the full path from the error message)replace
Localizations.localeOf(context, nullOk: true), with Localizations.maybeLocaleOf(context)
This is quick and dirty solution, but should hold until the package is fixed.
Solution 6:[6]
Note that if you want to fix this error for yourself or for a package that you are using, you can fix it the following way
In order to modify your code to use the new form of the APIs, convert all instances of calls that include nullOk = true as a parameter to use the maybe form of the API instead.
MediaQueryData? data = MediaQuery.of(context, nullOk: true);
becomes
MediaQueryData? data = MediaQuery.maybeOf(context);
More about how to migrate towards null-safety in the official documentation
Solution 7:[7]
Tried everything, in vain.
Finally, the updating a dependency worked for me.
This is what I updated in my pubspec.yaml file:
Removed
flutter_svg: ^0.18.0
and added
flutter_svg: ^0.20.0-nullsafety.3
Then, ran
flutter pub get
Solution 8:[8]
simply go into the file in .pub-cache and correct the argument from localeOf(context, nullOk: true) to localeOf(context)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | prahack |
| Solution 2 | Dharman |
| Solution 3 | jamesdlin |
| Solution 4 | Samad Talukder |
| Solution 5 | Ivan Pavlov |
| Solution 6 | Antonin GAVREL |
| Solution 7 | hackernewbie |
| Solution 8 | kuryz |
