Flutter: too many variables in widget constructor from paren widget - flutter

I have many callbacks and variables in stateless widget constructor from stateful parent. What solutions exist besides InheritedWidget?
[![enter image description here][1]][1]
class ContragentCreateTabs extends StatelessWidget {
ContragentCreateTabs({
required this.activeTab,
required this.onChangeTab,
required this.onChangeCurrency,
required this.onChangeFi,
required this.fis,
required this.currencies,
required this.numberController,
required this.fiCodeController,
required this.entityId,
this.chosenCurrency,
this.chosenFi,
Key? key,
}) : super(key: key);

Related

flutter const key in constructor - do we need to key in for each flutter class?

Was wondering do we need to always generate a const key for each dart/flutter stateless or stateful class?
Specifically, if I don't use the global key and for each widget, I create a separate unique key(used for testing purposes).
Main question: does generating
const NewWidget({Key? key}) : super(key: key);
bring any performance value or any other values as the Android studio dart language lint-> suggest to generate a const key.
class NewWidget extends StatelessWidget implements PreferredSizeWidget {
const NewWidget({Key? key}) : super(key: key);

How to Add and Use key in Custom widget constructors

I got notification warning (Not Error) about Use key in widget constructors. let say I have stateless class like this :
class TeaTile extends StatelessWidget {
final TheTea? tea;
const TeaTile({this.tea}); //the warning in hire!
#override
Widget build(BuildContext context) {
return Container();
}
}
the basic stateless format has a key like this :
class TeaTile extends StatelessWidget {
const TeaTile({ Key? key }) : super(key: key); //this one
#override
Widget build(BuildContext context) {
return Container();
}
}
I know how to disable the key rule use_key_in_widget_constructors: false. but I don't want to do it. so, how I add key in
final TheTea? tea;
const TeaTile({this.tea});
to solve the warning notification?
Update for Dart 2.17 using Super Initializers:
final TheTea? tea;
const TeaTile({ super.key, this.tea });
The super keyword in a constructor is a shortcut for the method below.
Older Dart versions:
final TheTea? tea;
const TeaTile({ Key? key, this.tea }) : super(key: key);
Basically a combination of both, you're still taking a named parameter key, that will pass it's value to the super constructor, and another named parameter tea that would set your final variable value.

What does super(key: key) do in the initialiser list for a constructor in flutter?

Take this code for example:
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
//returns widget
}
}
What I know
MyApp is constructor for the class which has field key. Initializer list consists of super(key: key). So does it mean that I am assigning the key of MyApp to the key of its super constructor?
Is the super constructor StatelessWidget?
Why do we need to assign key to the super?
What happens if I don't do this initializing?
I am new to flutter and am trying to understand how things work, and why.
Please help!
I think you should take a look at this post, it explain more than it is needed.
what is Key parameter in the constructor

Why is key required in constructor?

I have created class which extend StatefulWidget
class RegistrationPage extends StatefulWidget {
final String email;
const RegistrationPage({Key key, required this.email}) : super(key: key);
#override
_RegistrationPage createState() => _RegistrationPage();
}
The problem is android studio force me to put required before Key key. I googled some of examples how to pass values from screen to another screen and I have never seen that someone used required with Key.
I do it within:
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => RegistrationPage(email: email),
),
);
so just to pass email value. I need to make Key nullable to make it work.
Am I doing something wrong?
Because you're using null-safe Dart and key can't be null because it has a non-nullable type Key.
Solutions:
Use required
FooPage({required Key key});
Make key nullable.
FooPage({Key? key});
Remove key altogether.
FooPage();
I think your project is in null safety, with null safety a variable or object cannot be null execept if it is declared nullable.
Try adding a ? after Key:
class RegistrationPage extends StatefulWidget {
final String email;
const RegistrationPage({Key? key, required this.email}) : super(key: key);
#override
_RegistrationPage createState() => _RegistrationPage();
}
or you can simply delete the Key override:
class RegistrationPage extends StatefulWidget {
final String email;
const RegistrationPage({required this.email});
#override
_RegistrationPage createState() => _RegistrationPage();
}
I suggest you to read https://dart.dev/null-safety/understanding-null-safety
You're not doing anything wrong by making Key key nullable. The super constructors that you're passing the key to accept the nullable type.
So
const RegistrationPage({Key? key, required this.email}) : super(key: key);
is the norm as there is no reason to constrain the type by making it non-nullable and required.
If you have no need for keys with this widget, you can omit the super constructor and the key parameter completely.

Can any one explain this part?

class MyApp extends StatelessWidget {
final String title;
MyApp({Key key, this.title}) : super(key: key);
//... .
}
//I am a bigner and when I saw some videos , I saw this code.
//can any one explain it?
//I try to notice 3th line , but I don't find anything.
MyApp({Key key, this.title}) : super(key: key);
This is a constructor for the MyApp class.
It has two optional (because os the '{}') params.
key of the Type Key
title (with will be automatically assigned to the atribute tittle)
the constructor will call the constructor of the supper class passing the key param that was received.
Hope I could help you.