为什么 Riverpod 在项目中甚至无法初始化?找不到方法:'Error.throwWithStackTrace'

发布于 2025-01-11 01:21:23 字数 665 浏览 0 评论 0原文

因此,我尝试开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

初见终念 2025-01-18 01:21:23

Error.throwWithStackTrace 已在 Dart 2.16 中添加(Flutter 版本 2.10 自 2022 年 2 月 8 日起)。

https://api.flutter.dev/flutter/dart-core/Error /throwWithStackTrace.html

(检查 @Since 注释。)

如果您的 Flutter/Dart 版本低于该版本,您将收到您看到的错误。

可能有帮助的两个选项是:

  1. 指定依赖项的确切版本(不要在版本前使用 ^
  2. 将 Flutter/Dart 升级到至少 2.10/2.16
flutter upgrade

如果您有问题包(hooks_riverpod: ^2.0.0 在你的情况下)用^列出,它会使用不会破坏依赖关系的最新版本

我猜测当您创建一个新项目并使用相同的依赖项版本时,在初始 pub get 时,它会下载更新版本的包(< /em> 该包使用的依赖项的新版本) 到“pub 缓存”中,该缓存依赖于新的 Error API 方法。

您的项目将将此信息存储在:

<your_project_dir>/dart_tool/package_config.json

为包列出的 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:

  1. Specify an exact version of dependencies (don't use ^ ahead of version)
  2. upgrade Flutter/Dart to at least 2.10/2.16
flutter upgrade

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:

<your_project_dir>/dart_tool/package_config.json

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)

沙与沫 2025-01-18 01:21:23

对于任何在 firebase 中遇到“未找到方法:'Error.throwWithStackTrace'”的人。
尝试将其添加到您的 pubspec.yaml 中

dependency_overrides:
  firebase_messaging_platform_interface: 3.1.6

for anyone facing " Method not found: 'Error.throwWithStackTrace' " with firebase.
try to add this to your pubspec.yaml

dependency_overrides:
  firebase_messaging_platform_interface: 3.1.6
何其悲哀 2025-01-18 01:21:23

我看到了这个

尝试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.

雨巷深深 2025-01-18 01:21:23

正如 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.

时光瘦了 2025-01-18 01:21:23

尝试运行 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

只想待在家 2025-01-18 01:21:23

如果您在“flutter Upgrade”后仍然面临问题,或者您在 pubspec.yaml 中使用这行代码,

  dependency_overrides:   
    firebase_messaging_platform_interface: 3.1.6

您会收到此类错误,

java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)
    at com.sun.tools.javac.util.Assert.error(Assert.java:133)
    at com.sun.tools.javac.code.TypeAnnotations.annotationType(TypeAnnotations.java:231)
    at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.separateAnnotationsKinds(TypeAnnotations.java:294)
    at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitMethodDef(TypeAnnotations.java:1066)
    at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
    at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
    at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
    at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)

因此请尝试升级您的 gradle 版本,build.gradle 中的这一行

类路径'com.android.tools.build:gradle:4.1.0'

这样做后,它可能会要求你更改你的
gradle-wrapper.properties

distributionUrl=https://services.gradle.org/distributions/gradle-6.1.1-all.zip

distributionUrl=https://services.gradle.org/distributions/gradle-6.5-all.zip

还升级了这个包

firebase_core: ^1.10.0
firebase_messaging: ^11.1.0

if you are still facing the issues after "flutter upgrade" or you use this line of code in pubspec.yaml

  dependency_overrides:   
    firebase_messaging_platform_interface: 3.1.6

you receive this type of errors

java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)
    at com.sun.tools.javac.util.Assert.error(Assert.java:133)
    at com.sun.tools.javac.code.TypeAnnotations.annotationType(TypeAnnotations.java:231)
    at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.separateAnnotationsKinds(TypeAnnotations.java:294)
    at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitMethodDef(TypeAnnotations.java:1066)
    at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
    at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
    at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
    at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)

so Try to upgrade your gradle version, this line in your build.gradle

classpath 'com.android.tools.build:gradle:4.1.0'

after doing that it may be possible it ask you to change your
gradle-wrapper.properties

distributionUrl=https://services.gradle.org/distributions/gradle-6.1.1-all.zip

to

distributionUrl=https://services.gradle.org/distributions/gradle-6.5-all.zip

also upgarde this packages

firebase_core: ^1.10.0
firebase_messaging: ^11.1.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文