简体   繁体   English

使用 flutter_svg package 时如何修复此 nullOk 错误?

[英]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.我连接了 package,将其添加到 main.dart,我尝试编译应用程序,但出现此错误。 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

Follow the steps below请按照以下步骤操作

  1. add the following dependency添加以下依赖项
  flutter_svg: ^0.20.0-nullsafety.3
  1. run flutter clean command运行flutter clean命令
  2. run flutter pub get command运行flutter pub get命令
  3. run flutter run command or run the project运行flutter run命令或运行项目

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.每个人都是对的:放置flutter_svg: ^0.20.0-nullsafety.3flutter_svg: ^0.19.3 (以防您还不能迁移到 null 安全)是解决方案。 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:我想补充一点:如果您使用的 package 取决于flutter_svg,并且由于使用旧版本而仍然出现错误,您可以通过以下方式修复它:

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.我浪费了很多时间,因为flags: ^3.2.2 package 正在使用flutter_svg: ^0.19.1 ,它导致了同样的错误。

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 .问题是 Flutter 的版本启用了 null-safety(目前在编写时处于 beta 通道上), nullOk参数已删除Localizations.localeOf ,但您使用的是 package (在这种情况下, flutter_svg )仍然是使用nullOk

If you're using the null-safe version of Flutter, you should use null-safe versions of other packages too, if available.如果您使用的是 Flutter 的 null 安全版本,您也应该使用其他包的 null 安全版本(如果有)。 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.或者,您可以坚持使用没有 null 安全性的最新版本 Flutter(版本 1.22.x),直到迁移所有依赖包以实现 null 安全性。

I have faced the same problem.我遇到了同样的问题。 After I finally got the solution.在我终于得到解决方案之后。 Here Simple:这里简单:

At first you add Pre-Release the following dependency:首先,您添加 Pre-Release 以下依赖项:

flutter_svg: ^0.20.0-nullsafety.3

Then run flutter pub get command然后运行flutter pub get命令

Now you can run your project.现在你可以运行你的项目了。 See that there is no error.看看没有错误。

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)在本地缓存文件/lib/src/picture_provider.dart的第 57 行(请参阅错误消息中的完整路径)将Localizations.localeOf(context, nullOk: true)替换为Localizations.maybeLocaleOf(context)

This is quick and dirty solution, but should hold until the package is fixed.这是一个快速而肮脏的解决方案,但应该保持到 package 修复为止。

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请注意,如果您想为自己或您正在使用的 package 修复此错误,您可以通过以下方式修复它

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.为了修改您的代码以使用新形式的 API,请将包含 nullOk = true 作为参数的所有调用实例转换为使用 API 的可能形式。

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 更多关于如何在官方文档中迁移到 null-safety

Tried everything, in vain.尝试了一切,徒劳无功。

Finally, the updating a dependency worked for me.最后,更新依赖项对我有用。

This is what I updated in my pubspec.yaml file:这是我在 pubspec.yaml 文件中更新的内容:

Removed已移除

flutter_svg: ^0.18.0

and added并添加

flutter_svg: ^0.20.0-nullsafety.3

Then, ran然后,跑了

flutter pub get

simply go into the file in.pub-cache and correct the argument from localeOf(context, nullOk: true) to localeOf(context)只需将 go 放入文件 in.pub-cache 并将参数从 localeOf(context, nullOk: true) 更正为 localeOf(context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM