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

7,378

Solution 1

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)

Solution 2

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
Share:
7,378
monkeygoat
Author by

monkeygoat

Updated on January 03, 2023

Comments

  • monkeygoat
    monkeygoat over 1 year

    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) {
          ^
    
    • Yeasin Sheikh
      Yeasin Sheikh about 2 years
    • monkeygoat
      monkeygoat about 2 years
      @YeasinSheikh The fact is that i have written zero code. Just created a new flutter project in VSCode and it starts with the template of a counter app which works just fine in emulator and device. as soon as I install riverpod as mentioned in their docs that error shows up and I can't find where it comes from. No extra dependencies or code added. Also tried with a flutter clean and pub get and still nothing I can track to see where the error comes from. Thanks
  • Bhaumik Ghodasara
    Bhaumik Ghodasara about 2 years
    flutter upgrade worked for me. I was using 2.8.1 and upgraded to 2.10.2
  • monkeygoat
    monkeygoat about 2 years
    Thanks, exactly what was happening. It sure seemed like an "error" on their part, but I am more likely to make mistakes
  • mohammad mobasher
    mohammad mobasher about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.