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.
Related
class FooPage extends StatefulWidget {
const FooPage(Key? key) : super(key: key);
#override
State<FooPage> createState() => _FooPageState();
}
Is there any dart fix xyz command I can use to convert all the super(key:key) calls to super.key?
In other words:
Before:
const FooPage({Key? key}) : super(key: key);
After:
const FooPage({super.key});
Add this linter rule to your analysis_options.yaml file:
linter:
rules:
use_super_parameters: true
Now run
dart fix --apply --code use_super_parameters
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?
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
This question already has answers here:
Dart Multiple Constructors
(10 answers)
How can I create multiple constructors in dart?
(3 answers)
Closed 2 years ago.
class HomePage extends StatefulWidget {
final String uid;
HomePage({Key key, #required this.uid}) : super(key: key);
final FirebaseUser user;
HomePage({this.user});
#override
_HomePageState createState() => _HomePageState(uid);
}
The default constructor is already defined.
Try giving one of the constructors a name.dart(duplicate_constructor)
i want this two construters to pass on any one can help me in his
You're getting the error because you're trying to make two default constructor. Try making the second one a named constructor to fix the issue.
Note : Dart doesn't support's constructor and method overloading. That's why it comes with named methods that makes them more readable and easy to manage.
class HomePage extends StatefulWidget {
final String uid;
HomePage({Key key, #required this.uid}) : super(key: key);
final FirebaseUser user;
HomePage.user({this.user});
#override
_HomePageState createState() => _HomePageState(uid);
}
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;
}