monkeygoat
monkeygoat

Reputation: 43

Why is riverpod failing even to initialize in project? Method not found: 'Error.throwWithStackTrace'

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) {
      ^

Upvotes: 4

Views: 11080

Answers (6)

benten
benten

Reputation: 790

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

Upvotes: 0

Priyanshu Paliwal
Priyanshu Paliwal

Reputation: 2388

Try running flutter upgrade

By this it will upgrade your flutter and dart SDK version and hope it resolves your issue

Upvotes: 0

jamesdlin
jamesdlin

Reputation: 89995

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.

Upvotes: 0

Baker
Baker

Reputation: 28010

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)

Upvotes: 16

Kamal Cherkaoui
Kamal Cherkaoui

Reputation: 63

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

Upvotes: 3

jeiea
jeiea

Reputation: 2405

I saw this.

How about trying flutter pub upgrade?

If it is not resolved, then check analyzer version and check which dependency locks that.

Upvotes: 0

Related Questions