Home > front end >  Why is riverpod failing even to initialize in project? Method not found: 'Error.throwWithStackT
Why is riverpod failing even to initialize in project? Method not found: 'Error.throwWithStackT

Time:02-25

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

CodePudding user response:

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

Since hooks_riverpod: ^2.0.0 dependency 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 hooks_riverpod (or a newer version of a dependency hooks_riverpod 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

Normally this type of error shouldn't happen, but I believe the author of the API change / riverpod forgot to update version numbers to 3.x since the min Dart SDK version should have changed from 2.12 to 2.16. So, what was not supposed to be a breaking change update, actually is a breaking change update. The ^ prefix on dependency versions is supposed to prevent these types of issues... but only if version numbering correctly identifies breaking changes.

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)

CodePudding user response:

I saw this.

How about trying flutter pub upgrade?

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

  • Related