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

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

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:
Specify an exact version of dependencies (don't use ^ ahead of version)
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)

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

I saw this.
How about trying flutter pub upgrade?
If it is not resolved, then check analyzer version and check which dependency locks that.

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.

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

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

Related

Flutter Basics: Error: Type 'AssetBundle' not found. AssetBundle' isn't a type

After I added the flutter_svg: ^0.23.0+1 in pubsec.yaml getting these error.
Please check your Flutter version.
At flutter_svg github issue page, there is a same issue and solution comment.
https://github.com/dnfield/flutter_svg/issues/610
If you are on stable channel, make sure Flutter version is at least 2.5.0. You need at least 2.4.0 pre for the latest version of this package
If you do not want to upgrade your flutter, then this is also a viable solution,
use flutter_svg: ^0.22.0
https://github.com/dnfield/flutter_svg/issues/610#issuecomment-974614148
I'm on 2.2 and it is working very well.

Flutter: build runner throws a precompile error

I am using analyser 1.7.1. The latest build_runner build command generates the following error.
flutter packages pub run build_runner build Failed to precompile
build_runner:build_runner:
../../../sdk/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.7.1/lib/src/error/best_practices_verifier.dart:1998:14:
Error: A non-null value must be returned since the return type
'String' doesn't allow null. String get displayString {
The usual flutter clean and pub cache repair commands don't seem to be fixing the problem, and displayString doesn't appear anywhere in my codebase.
There's an issue open 9 days ago. Here's the key part:
Right now, the current state of affairs is that:
package:analyzer 1.7.0 requires package:meta ^1.4.0
package:analyzer 1.7.1 has the same contents as 1.7.0, but requires package:meta ^1.3.0
Flutter stable pins package:meta to 1.3.0
I'm uncertain how we're running into the exceptions above - the two most recent versions of analyzer are pretty explicit about which version of meta they need.
#edlman do you have any dependency_overrides in your pubspec?
you're right, I'm using 3rd party pkgs which depend on meta 1.4.0 so I put it to dependency_overrides to solve the collision. It didn't come to my mind it cause such a problem.
I've changed the override to 1.3.0, it works fine, no problem yet
So I'd suggest checking whether or not there's a dependency_overrides in your pubspec, too.
It's a problem with analyser 1.7.1.
Add
dependency_overrides:
analyzer: 1.7.0
to pubspec.yaml.
There are more details available in raina77ow's answer.

How to get latest version of package in flutter create template

I am modifying my default template file from my flutter installation directory here
C:\src\flutter\flutter\packages\flutter_tools\templates\app\pubspec.yaml.tmpl
dependencies:
#omitted
cupertino_icons: ^0.1.3
mobx: latest
flutter_mobx: latest
I am getting this error
Invalid version constraint: Could not parse version "latest". Unknown text at "latest".
How do i get the latest version instead of specifying specific version like ^1.x.x
There is no latest, you have to specify the version number or you can use any:
any
The string any allows any version. This is equivalent to an empty version constraint, but is more explicit. Although any is allowed, we don’t recommend it.
https://dart.dev/tools/pub/dependencies
To update your dependencies to the latest version you can do the following:
Use pub upgrade to update to the latest package versions that your pubspec allows. To identify dependencies in your app or package that aren’t on the latest stable versions, use pub outdated.
try this after the package that you want the latest
http: ^{latest version}

Dart 2.6 and Flutter, Upgrading Dependency

Using Flutter, I am trying to update my dart dependency 2.6.0 but I get an error
Because *project_name* requires SDK version >=2.6.0 <3.0.0, version solving failed.
pub get failed (1)
exit code 1
I want to use the new features that dart offers, such as Extension Methods. How could I go about upgrading my dependency? I have installed the newest stable version of dart on my computer, but regardless I still get that error.
A couple of things that might help:
From this issue, you can try ensuring that you've updated enviroment in your pubspec.yaml file like so:
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
Another thing that might help is just double checking the steps in this post or on the dart installation page to ensure that you've updated to the latest version correctly.

google_maps_flutter and AndroidX migration

I am working on a project that requires google_maps_flutter package. When selecting the option Run -> Flutter run in Release Mode I receive:
The Gradle failure may have been because of AndroidX incompatibilities in this Flutter app.
See for more information on the problem and how to fix it.
If I follow the documentation to Avoiding AndroidX from the official page:
https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility#avoiding-androidx
I have the following issue:
The last available version pre AndroidX for google_maps_flutter is 0.1.0
Seems that this version does not exist.
Log Error:
Because depends on google_maps_flutter 0.1.0 which doesn't match any versions, version solving failed.
pub get failed (1)
Is there a solution for this problem?
You are facing the issue because the package version is too old.
Latest version for google_maps_flutter is 0.5.13
Follow instructions which are mentioned in the Google maps flutter package here
Follow instructions from here to migrate your flutter app to AndroidX.