为什么 Riverpod 在项目中甚至无法初始化?找不到方法:'Error.throwWithStackTrace'
因此,我尝试开始使用 Riverpod 并使用“点击和计数器”默认示例创建一个新的 flutter 项目。 一旦我添加了 pubspec
flutter_hooks: ^0.18.0
hooks_riverpod: ^2.0.0
并且
导入'package:hooks_riverpod/hooks_riverpod.dart';
我在调试控制台上收到此错误,但无法找出问题所在
: Error: Method not found: 'Error.throwWithStackTrace'.
../…/framework/provider_base.dart:904
Error.throwWithStackTrace(error, chain);
^^^^^^^^^^^^^^^^^^^
: Error: A non-null value must be returned since the return type 'Never' doesn't allow null.
../…/framework/provider_base.dart:898
Never _rethrowProviderError(Object error, StackTrace stackTrace) {
^
So i am trying to get started with riverpod and creating a new flutter project with the "click and counter" default sample.
As soon as I add on the pubspec
flutter_hooks: ^0.18.0
hooks_riverpod: ^2.0.0
And
import 'package:hooks_riverpod/hooks_riverpod.dart';
I get this error on the debug console and can't figure it out what is the problem
: Error: Method not found: 'Error.throwWithStackTrace'.
../…/framework/provider_base.dart:904
Error.throwWithStackTrace(error, chain);
^^^^^^^^^^^^^^^^^^^
: Error: A non-null value must be returned since the return type 'Never' doesn't allow null.
../…/framework/provider_base.dart:898
Never _rethrowProviderError(Object error, StackTrace stackTrace) {
^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Error.throwWithStackTrace
已在 Dart 2.16 中添加(Flutter 版本 2.10 自 2022 年 2 月 8 日起)。https://api.flutter.dev/flutter/dart-core/Error /throwWithStackTrace.html
(检查
@Since
注释。)如果您的 Flutter/Dart 版本低于该版本,您将收到您看到的错误。
可能有帮助的两个选项是:
^
)如果您有问题包(
hooks_riverpod: ^2.0.0
在你的情况下)用^
列出,它会使用不会破坏依赖关系的最新版本。我猜测当您创建一个新项目并使用相同的依赖项版本时,在初始
pub get
时,它会下载更新版本的包(或< /em> 该包使用的依赖项的新版本) 到“pub 缓存”中,该缓存依赖于新的 Error API 方法。您的项目将将此信息存储在:
为包列出的
min Dart SDK
版本通常应从 2.12 更改为 2.16。 (更新:现已更新)这会提示我们需要更新如果我们看到我们的构建失败,我们的 Flutter/Dart 就会出现。在此答案的早期版本中,我注意到包依赖项版本上的
^
前缀应该可以防止这些类型的问题,但我不再确定它是否旨在涵盖这种情况底层平台需要更新(而不是包本身的 API 发生重大变化)。不管怎样,乍一看,当包版本依赖于 2.0.0 发布时不存在的新核心 Dart API 方法时,从 2.0.0 升级到 3.0.0 可能是有意义的。
注释
Riverpod 的作者还为
Error.throwWithStackTrace
编写了新的 API,因此最新版本的 hooks_riverpod 可能正在使用最新的 API 更改。 (您列出的版本 2.0.0 目前是预发布版本)。您可以在 pubspec.yaml 中尝试早期版本的 Riverpod(例如 1.0.3)Error.throwWithStackTrace
was added in Dart 2.16 (Flutter version 2.10 from Feb 8, 2022).https://api.flutter.dev/flutter/dart-core/Error/throwWithStackTrace.html
(Check the
@Since
annotation.)If your Flutter/Dart version is below that, you'll get the error you saw.
Two options that may help are:
^
ahead of version)If your problematic package (
hooks_riverpod: ^2.0.0
in your case) is listed with^
, it'll use the latest version that doesn't break dependencies.I'm guessing that when you created a new project and used that same dependency version, upon initial
pub get
it downloaded a newer version of the package (or a newer version of a dependency that that package uses) into "pub cache" which is relying on the new Error API method.Your project will store this information in:
The
min Dart SDK
version listed for the package should normally have changed from 2.12 to 2.16. (update: it's been updated now) This would give us a hint that we need to update our Flutter/Dart if we see our build failing.In an earlier version of this answer, I noted that the
^
prefix on package dependency versions is supposed to prevent these types of issues, but I'm no longer certain it was meant to cover this situation where the underlying platform needs to be updated (as opposed to a breaking change in the API of the package itself).Anyways, at first glance, it might make sense to go from 2.0.0 to 3.0.0 for a package version # when it depends on a new core Dart API method that did not exist when 2.0.0 was published.
Notes
The author of riverpod also wrote the new API for
Error.throwWithStackTrace
so likely the latest version of hooks_riverpod is using the latest API changes. (The version you're listing 2.0.0 is pre-release currently). You could try earlier versions of riverpod in your pubspec.yaml (such as 1.0.3)对于任何在 firebase 中遇到“未找到方法:'Error.throwWithStackTrace'”的人。
尝试将其添加到您的 pubspec.yaml 中
for anyone facing " Method not found: 'Error.throwWithStackTrace' " with firebase.
try to add this to your pubspec.yaml
我看到了这个。
尝试
flutter pub升级
怎么样?如果未解决,请检查分析器版本并检查哪个依赖项锁定了该版本。
I saw this.
How about trying
flutter pub upgrade
?If it is not resolved, then check
analyzer
version and check which dependency locks that.正如 Baker 的回答中所述,最近在 Dart 2.16 中添加了
Error.throwWithStackTrace
。如果您收到有关某些包未找到
Error.throwWithStackTrace
的错误,则包作者忽略了更新其包的最低 Dart SDK 版本。您应该向软件包维护者报告这一情况。同时,您应该使用有问题的包的早期版本或使用较新版本的 Dart SDK。As noted in Baker's answer,
Error.throwWithStackTrace
was recently added in Dart 2.16.If you're getting an error about
Error.throwWithStackTrace
not being found for some package, the package author neglected to update their package's minimum Dart SDK version. You should report that to the package maintainer. In the meantime, you should use an earlier version of the problematic package or use a newer version of the Dart SDK.尝试运行 flutter Upgrade
通过此操作,它将升级您的 flutter 和 dart SDK 版本,并希望它能解决您的问题
Try running flutter upgrade
By this it will upgrade your flutter and dart SDK version and hope it resolves your issue
如果您在“flutter Upgrade”后仍然面临问题,或者您在 pubspec.yaml 中使用这行代码,
您会收到此类错误,
因此请尝试升级您的 gradle 版本,build.gradle 中的这一行
这样做后,它可能会要求你更改你的
gradle-wrapper.properties
到
还升级了这个包
if you are still facing the issues after "flutter upgrade" or you use this line of code in pubspec.yaml
you receive this type of errors
so Try to upgrade your gradle version, this line in your build.gradle
after doing that it may be possible it ask you to change your
gradle-wrapper.properties
to
also upgarde this packages