dart pub upgrade not taking my dependencies constraints into consideration - flutter

dependencies:
test: ^1.19.3
I had my test dependency purposely downgraded.In my package, test's constraints were ^1.19.3. After I ran dart pub upgrade, it upgraded my test to 1.20.0 and it was visible in pubspec.lock even if my app depended on ^1.19.3 it kept it as it is and ignored it.

This is as intended: the ^ symbol indicates packages up to the next major version (in your example up to version 2.0.0) will upgrade with pub upgrade. If you want to lock to an exact version (not recommended) then you just remove the caret symbol (^).
See here for more details.

Related

flutter migration to null-safety - how fix "Bad state: Error: package has unmigrated dependencies"?

I have a pretty big Flutter project, I'm currently adding null-safety support. My project consists of several parts - the "core" and "client".
"Client" used "core"
environment:
sdk: ">=2.10.0 <3.0.0"
.....
app_core:
path: ../core
First I updated the core with command
dart migrate
Now I when run "dart migrate"(in core) - I get this result:
All sources appear to be already migrated. Nothing to do.
Now I try to update the "client" part, I run the following commands
dart pub outdated --mode=null-safety
[✗] indicates versions without null safety support.
[✓] indicates versions opting in to null safety.
All your dependencies declare support for null-safety.
Next step:
dart pub upgrade --null-safety
Changed 1 dependency!
1 package is discontinued.
24 packages have newer versions incompatible with dependency constraints.
Try `dart pub outdated` for more information.
No changes to pubspec.yaml!
Next step:
dart pub get
When I run
dart migrate
I get these messages:
Bad state: Error: package has unmigrated dependencies.
Before migrating your package, we recommend ensuring that every library it
(either directly or indirectly) has been migrated to null safety, so
that you will be able to run your unit tests in sound null checking mode. You
are currently importing the following non-null-safe libraries:
...//file list
..............
Please upgrade the packages containing these libraries to null
safe versions
before continuing. To see what null safe package versions are
available, run
the following command: `dart pub outdated --mode=null-safety`.
To skip this check and try to migrate anyway, re-run with the
flag
`--skip-import-check`.
"file list" - is a large list of files from "client" (I did not find any regularity in it). Also, these files have no errors.
What is the reason for these warnings?
How can I fix it? Any advice - I will be grateful.

Upgrading projects Flutter SDK, migration to null-safety - compile time errors in packages

We have decided to upgrade the Flutter version from 1.22 to 2.0 (migrating to null-safety) in an existing project.
We checkout Flutter v2.0.0 and then run the following command:
dart pub upgrade --null-safety
Flutter nicely resolves all dependency conflicts and sets new versions for packages.
Without changing min environment SDK in pubspec.yaml (staying at ">2.10.0 <3.0.0") we fix all breaking changes in the code.
All errors in vs code disappear so we can compile the code. But, when we do compile we get errors from packages e.g. reorderables, fl_chart, flutter_svg etc. stating some classes are undefined. It seems like packages were compiled with a higher SDK version and they weren't set min SDK version to match the one that they were compiled with.
So the package states it can be compiled with Flutter 2.10.0 but in reality, it fails to compile because in this version of Flutter it is missing some necessary imports.
My question is, is this a problem of packages or a problem in my way of processing with migration?
Is it that packages are lacking properly set min SDK?
My conclusion is that most packages are lacking correctly set SDK version constraints. It seems authors usually compile packages for a certain version and do not check min SDK version required. When running dependency resolver it seems to solve dependencies but in compile time it fails.

Version solving for certain package versions failed in flutter

I have received this kind error a couple of time before with different packages, usually i just try a combination of different versions till i finally get a match that works. Is there a way to know what package versions are compatible instead of using the trial error approach which is outright time consuming and exhausting.
Initially i though it could be that one of the packages depends on the other, like in this case maybe flutter_svg depends on a different version of flutter_luban so the version in my pubspec clashes with it, but none of these packages depend on one another after viewing the respective package dependencies.
pub get failed (1; So, because sakaHapa depends on both
flutter_svg ^0.17.4 and flutter_luban ^0.1.13, version solving
failed.)
You can use 'pub outdated' command like below.
https://dart.dev/tools/pub/cmd/pub-outdated
Here is column what means.
Current
The version used in your package, as recorded in pubspec.lock. If the package isn’t in pubspec.lock, the value is -.
Upgradable
The latest version allowed by your pubspec.yaml file. This is the version that dart pub upgrade resolves to. The value is - if the value in the Current column is -.
Resolvable
The latest version that can be resolved, when combined with all other dependencies. This version corresponds to what dart pub upgrade gives you if all version constraints in pubspec.yaml are unbounded. A value of - means that the package won’t be needed.
Latest
The latest version of the package available, excluding prereleases unless you use the option --prereleases.
flutter pub outdated

Flutter version constraints missunderstanding

is that means that if i use a package version ^2.0.1 that means if the developer of this certain packages update his package to 3.0.0, my application, which including this package will lose it although my application is released?
No, dependencies are only resolved at compile time. Once your application is released, those obviously won't change.
No, if you have ^2.0.1 in your pubspec.yaml and run pub get the most recent version will be locked into pubspec.lock - let's say your dependency has released version 2.0.4 then pubspec.lock will contain that information. For applications it is hence recommended that you add pubspec.lock to your version control. Subsequent pub get will always fetch the locked 2.0.4 until you either change something in your dependencies or run pub upgrade. If you run pub upgrade and the package author has release 2.3.0 this new version will be included. On the other hand if the package author releases 3.0.0 it will still not be used, because ^2.0.1 means the same as >=2.0.1 <3.0.0

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.