Trying to use the super-parameters in old project after upgrading to flutter 3.0.0 - flutter

I am trying to use the new feature of dart (super-parameters) in my project, but every time the build is failed.
this is my code after the migration:
class ChildClass extends ParentClass {
int? paramOne;
String? paramTwo;
ChildClass({
this.paramOne,
this.paramTwo,
super.paramThree,
super.paramFour,
});
// ...
this is the error message:
[ ] dart_project/lib/src/model/app_model.dart:407:5: Error: The 'super-parameters' language feature is disabled for this library.
[ +1 ms] Try removing the package language version or setting the language version to 2.17 or higher.
[ ] super.paramThree,
[ ] ^^^^^
pubsec.yaml:
environment:
sdk: '>=2.17.0 <3.0.0'

You can run flutter clean to clear the cache and then run flutter pub get which will get all the dependencies without cache.

Related

Bitrise flutter build keeps using older versions of flutter/dart

I have a flutter app that I am trying to build through bitrise.
I keep getting a tonne of errors along the lines of
Error: The 'super-parameters' language feature is disabled for this library.
Try removing the package language version or setting the language version to 2.17 or higher.
super.key
and it is blocking me from analyzing and building the app.
Now the issue is, I am using flutter install with upgrade set to true , and have sdk target in my pubspec.yaml as
Environment:
sdk: ">=2.17.6 <3.0.0"
I disabled the caching steps, and tried to create a new deployment pipeline, to no avail.
In your workflow, add Flutter Install, Then set your desired flutter version on Input Variables sections's Flutter SDK git repository version field. e.g. 2.8.0.

Unable to generate g.dart with retrofit flutter

I am migrating my flutter project to 3.3.0 and i am calling API with retrofit which was working fine, after upgrading all packages i delete .g.dart file and run the following command to rebuild the .g.dart file
flutter pub run build_runner build it thrown the following error.
Error: No named parameter with the name 'autoCastResponse'.
autoCastResponse: annotation?.peek('autoCastResponse')?.boolValue,
^^^^^^^^^^^^^^^^
/E:/ahmad/Android/Android%20Bundle%20Setup/flutter/.pub-cache/hosted/pub.dartlang.org/retrofit-3.3.0/lib/http.dart:66:9: Context: Found this candidate, but th
e arguments don't match.
const RestApi({
^^^^^^^
/E:/ahmad/Android/Android%20Bundle%20Setup/flutter/.pub-cache/hosted/pub.dartlang.org/retrofit_generator-4.1.2/lib/src/generator.dart:494:27: Error: The getter 'autoCastResponse' isn't defined for the class 'RestApi'.
- 'RestApi' is from 'package:retrofit/http.dart' ('/E:/ahmad/Android/Android%20Bundle%20Setup/flutter/.pubcache/hosted/pub.dartlang.org/retrofit-3.3.0/lib/http.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'autoCastResponse'.
(clientAnnotation.autoCastResponse ?? true);
^^^^^^^^^^^^^^^^
[INFO] Precompiling build script... completed, took 900ms
[SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
pub finished with exit code 78
I also try this command flutter packages pub run build_runner build --delete-conflicting-outputs
But the result remains the same.
Here is how my abstract class looks like,
part 'rest_client.g.dart';
#RestApi(baseUrl: "https://myapi.com/")
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;
#POST("/Flag")
Future<LoginResponse> login(#Body() LoginRequest loginRequest)`;}`
These are my packages version:
retrofit: ^3.3.0
dev_dependencies:
flutter_test:
sdk: flutter
retrofit_generator: ^4.1.2
build_runner: ^2.2.1
json_serializable: ^6.4.1
dependency_overrides:
analyzer: '5.1.0'
According to this githubLink
retrofit: 3.3.0 had bugs and the author forgot to publish the generator accordingly. So after resolving that bug using retrofit: ^3.3.1 and retrofit_generator: 4.2.0 is working fine
You will need to upgrade the package retrofit_generator to 4.2.0
you need to take care of the file naming your file name should be like what you will generate if you will generate [users.g.dart] your file name should be [ users.dart] or it will not create the file
and also upgrade your package to retrofit_generator: ^4.0.3+1

Using the triple-shift (>>>) operator in Dart

Supposedly Dart 2.14 included the triple shift operator (>>>), but when I try to use it I get an error:
print(0xff >>> 1);
The compiler highlights the last > of the three and says:
Expected an identifier.
This is true on my local machine and in DartPad. Both are using the version of Dart 2.14 shipped with Flutter 2.5.
Am I doing something wrong or is this a problem with the release?
You can refer to my related answer here.
Any language features that are introduced in a given version require that version as the minimum constraint for your project. This means you need to update your pubspec.yaml:
environment:
sdk: '>=2.14.0 <3.0.0'
Now, you can use:
var foo = 42;
foo >>> 2;
If you compile this code, required Dart SDK version 2.14.1.
Dart language site’s documentation and examples use version 2.13.4 of the Dart SDK.
https://dart.dev/tools/sdk.
So when you are trying with Flutter, It uses SDK version less than 2.14.1.
Download the latest Flutter SDK
https://flutter.dev/docs/get-started/install/windows and replace
the old one. Update your pubspec.yaml file
environment:
sdk: ">=2.14.0 <3.0.0"
To run your code without Flutter app:
Download latest Dart SDK from https://dart.dev/tools/sdk/archive
Create a project with Android Studio IDE.
Create a new Flutter project.
Select Dart from the left menu.
Locate Dart SDK path, that you downloaded(Must be extracted)
Provide your project name and location.
Create a Dart file and write these lines of code.
void main() {
final value = 0x22;
print("Current value : $value");
print("Unsigned shift right value is : ${(value >>> 4)}");
}
Output is
F:/Dart/dart-sdk/bin/dart.exe --enable-asserts F:\Flutter\TestDartLanguage\main.dart
Current value : 34
Unsigned shift right value is : 2
Process finished with exit code 0
Enjoy happy coding. Thanks

Flutter Freezed Expected to find ')'

After upgrading my Flutter, Freezed appears to generate files that have errors in them for my Bloc/Cubit files.
Pubspec.yaml has all the latest versions for the packages.
part of 'test_cubit.dart';
#freezed
abstract class TestState with _$TestState {
const factory TestState.initial() = _Initial;
const factory TestState.loaded(String someValue) = _Loaded;
}
Would be generated as (only excerpt where error occurs)
/// #nodoc
class _$TestStateTearOff {
const _$TestStateTearOff();
_Initial initial() {
return const _Initial();
}
_Loaded loaded( String* someValue) {
return _Loaded(someValue,);
}
}
Image of the errors
When I delete the * it all works just fine. Can anyone shed some light on this please?
This was answered on GitHub in the end.
The issue was identified after running a check to ensure the the application was fully migrated to null-safety.
$ dart pub upgrade --null-safety
null-safety compatible versions do not exist for:
- build_runner
When running flutter pub run build_runner build it yielded an error
Error: Cannot run with sound null safety, because the following dependencies don't support null safety:
- package:build_runner_core
- package:build_runner
- package:build_config
- package:build_daemon
- package:code_builder
After editing the pubspec.yaml and setting a higher environment it worked. This was not automatically done as part of the upgrade (or new applications).
environment:
sdk: ">=2.12.0-0 <3.0.0"

Turn off Null Safety for previous Flutter Project?

I want to upgrade my flutter to get new features such as null safety, but I didn't want my previous project to affect them. I want new changes only for my new flutter project, I want to run my old project similar to the old way. Is there any way? Please guide me through it.
Thank You
Setting SDK constraints in your old project's pubspec.yaml file should be enough.
For example, the following, does not have null safety enabled:
environment:
sdk: ">=2.11.0 <3.0.0"
You can also specify at the top of your Dart file to disable null checks for that file.
// #dart=2.9
The above answer wasn't working for me after I upgraded to Dart v2.12 on the beta channel. So I found these options:
You can add this to the top of any dart file to disable null-safety checks.
// #dart=2.9
or similar to the answer above, I had to include a version prior to 2.12 to disable null safety. You would edit this line in the pubspec.yaml file.
environment:
sdk: ">=2.11.0 <3.0.0"
You can run flutter project without null safety with --no-sound-null-safety option with flutter run.
Also you can add this as an argument in launch.json in VSCode
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"args": [
"--no-sound-null-safety"
],
},
]
your project might be developed using flutter older(version below 2.+). The major change in 2.x.x dart version is enabling null safety. At this moment, a lot of libs on pub.dev have been upgraded to the null safety feature.
But your old project might have some libs which are still not updated to null safety. So, your project might have mixture of both. In this case #miguel answer is partially valid (Defining sdk: ">=2.7.0 <3.0.0" constraint). To run your project you also need to unsound the null-safety. like by running following command
flutter run --no-sound-null-safety
or add this command in configuration by go to Run->Edit Configurations. it will open the following popup and the highlighted string same as
**Recommended: **Update your project to null safety. Read more about null-safety
I had the same problem and after doing a lot of downgrading and upgrading (especially when the old project needed to be built with the old version of Flutter and build_runner) I found out about Flutter Version Manager check the git repo here: https://github.com/leoafarias/fvm. The best thing about this is you can specify which version you want to use per project.
Following comes from the instructions in the repo:
To activate globally run pub global activate fvm
To install a specific version of Flutter run fvm install <version>
Then go into the root of the project and run fvm use <version>
Et voila! Hope it helps 👍.
Check the repo for more commands like fvm use <version> --global to easily switch global versions and more interesting stuff.
You can declare the variables using var or dynamic and now the compiler won't check the variable type.
var answerText;
dynamic answerColor;
Answer({this.answerText, this.answerColor});
And if your try to build without the null safety checking then use this comment line // #dart=2.9
in the main.dart first line.
// #dart=2.9
import 'package:flutter/material.dart';
void main() async {
runApp(
const MyApp()
);
}
Delete the following line of code from gradle file or just comment it out to run the code without the Gradle Exeception-
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}