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


Solution 1:[1]

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

Solution 2:[2]

I saw this.

How about trying flutter pub upgrade?

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

Solution 3:[3]

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.

Solution 4:[4]

Try running flutter upgrade

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

Solution 5:[5]

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

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 Kamal Cherkaoui
Solution 2 jeiea
Solution 3 jamesdlin
Solution 4 Priyanshu Paliwal
Solution 5 benten