Flutter calling StatefulWidget's constructor caused expression can't be evaluated - flutter

I'm creating a stateful widget, and trying to call the superclass construction using super(key: key. However, I'm getting an error saying:
class ArticlesPage extends StatefulWidget {
ArticlesPage({Key key}) {
super(key: key);
}
#override
_ArticlesPageState createState() => _ArticlesPageState();
}

For dart, super should be called in the initializer list. Ex:
ArticlesPage({Key key}) : super(key: key);
If you also want to initialize fields here, you should do this before the super call. super should always be last in the initializer list.

If you want to save key from --> Key key as a class variable also.
In this case, you will get a default GlobalKey value if it's not passed during constructor initialize.
AppStateFullWidget({Key key}):super(key: key ?? GlobalKey()){
_key = super.key;
}

Related

error: 1 positional argument(s) expected, but 0 found

When I want to declare this function in my main.dart
class MapEqualColorMappingPage extends SampleView {
const MapEqualColorMappingPage(Key key) : super(key: key);
#override
_MapEqualColorMappingPageState createState() =>
_MapEqualColorMappingPageState();
}
class _MapEqualColorMappingPageState extends SampleViewState {
List<_CountryTimeInGMT> _timeZones;
MapShapeSource _mapSource;
...
}
i get the following error
error: 1 positional argument(s) expected, but 0 found.
'/map': (context) => MapEqualColorMappingPage(), //The error is HERE
what should id ?
Instead of MapEqualColorMappingPage(Key key) write MapEqualColorMappingPage({Key? key})
class MapEqualColorMappingPage extends SampleView {
const MapEqualColorMappingPage({Key? key}) : super(key: key);
#override
_MapEqualColorMappingPageState createState() =>
_MapEqualColorMappingPageState();
}
class _MapEqualColorMappingPageState extends SampleViewState {
List<_CountryTimeInGMT> _timeZones;
MapShapeSource _mapSource;
...
}
Here
const MapEqualColorMappingPage(Key key) : super(key: key);
you are stating that the constructor must have an argument of type Key, and here
'/map': (context) => MapEqualColorMappingPage()
you're trying to use the same constructor without any kind of argument.
You can:
add a Key argument when you use the constructor
remove the Key argument from the constructor declaration
put the Key argument in the declaration in curly braces in order to make it optional
You are not passing Key parameter.
const MapEqualColorMappingPage(Key? key) : super(key: key);
Mark it as optional Key?

Passing Arguments to super from the widget in flutter

Im trying to pass arguments from my widget's state into a super class, but i cannot access the "widget." from the initialization list.
if i do pass it from the variables and accept an additional parameter in the state's ctor, i get the lint error:
no_logic_in_create_state
Here's my code:
class TransferPage extends View {
final String screenId;
TransferPage(this.screenId, {Key? key}) : super(key: key);
#override
// ignore: no_logic_in_create_state
State<TransferPage> createState() => TransferPageState(screenId);
}
class TransferPageState extends ViewState<TransferPage, TransferController> {
final String screenId;
TransferPageState(this.screenId)
: super(TransferController(GetTransferDataUsecaseParams(screenId)));
I want to pass the id into the "super" ctor,
What's the best way to go about it?

Multiple constructor in flutter

I try to add multiple constructor in my code but it shows error. help me to solve this.
code :
class NoteModify {
const NoteModify({Key? key}) : super(key: key);
String NoteID;
NoteModify(String x) {
NoteID = x;
}
}
I need to use both constructor. Because I am working with 2 buttons and one button for navigate without sending data to another activity and one button for navigate with data to another activity.
As #Jigar Fumakiya said in his answer, dart doesn't support overloading.
You need to use differently named constructors to fix the error
NoteModify is already declared in this scoped
However, you have another issue, you declared a const constructor and String NoteID is not a final. To declare a const constructor, all the fields must be final.
That is why you get the error
Error: Constructor is marked as 'const' so all fields must be final
If you need the const constructor:
class NoteModify{
const NoteModify({Key? key}) : NoteID = null, super(key: key);
NoteModify.formId({this.NoteID});
final String? NoteID;
}
If you need NoteID to be a variable instead, you'll have to remove the const keyword:
class NoteModify{
NoteModify({Key? key}): super(key: key);
NoteModify.formId({this.NoteID});
String? NoteID;
}
Dart doesn't support methods/constructor overloading. But you can have multiple named constructor
Here
class NoteModify{
String NoteID;
NoteModify({Key key}) {
// main constructor
}
NoteModify.formId({this.NoteID}){
//Here is the named constructor
}
}

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

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.