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?
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
So basically I got an error that says
The instance member 'key' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression when I try to make a StatefulWidget as shown below
class UserPage extends StatefulWidget {
UserData userData;
UserPage(this.userData) : super(key: key);
#override
State<StatefulWidget> createState() => new _UserPageState(userData);
}
any solution for this one?
I tried to add 'late' at every point but it doesn't seem to work.
You should do something like this:
class UserPage extends StatefulWidget {
const UserPage({required this.userData, Key? key}) : super(key: key);
final UserData userData;
#override
State<UserPage> createState() => _UserPageState();
}
The key param is not always required. So you can just delete the super part.
class UserPage extends StatefulWidget {
UserData userData;
UserPage(this.userData);
#override
State<StatefulWidget> createState() => new _UserPageState(userData);
}
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.
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;
}
I try to create some custom widgets with some parameters in the constructor. This widget has some optional and required parameters.
how can make Function type parameter optional in my Widget.
class TextInputWithIcon extends StatefulWidget {
final String iconPath;
final String placeHolder;
final Function(bool) onFocusChange;
const TextInputWithIcon(
{Key key,
#required this.iconPath,
this.placeHolder = "",
this.onFocusChange})
: super(key: key);
#override
_TextInputWithIconState createState() => _TextInputWithIconState();
}
class _TextInputWithIconState extends State<TextInputWithIcon> {
#override
Widget build(BuildContext context) {
return MY_WIDGET;
}
}
Optional parameters can be either positional or named, but not both.
Named parameters are optional by default so you don't have to assign the default value.
If a parameter is optional but can’t be null, provide a default value.
With null safety
class TextInputWithIcon extends StatefulWidget {
final String iconPath;
final String placeHolder;
final Function(bool)? onFocusChange; // nullable and optional
const TextInputWithIcon(
{Key? key,
required this.iconPath, // non-nullable and required
this.placeHolder = "", // non-nullable but optional with a default value
this.onFocusChange, // nullable and optional
})
: super(key: key);
#override
_TextInputWithIconState createState() => _TextInputWithIconState();
}
Without null safety
const TextInputWithIcon(
{Key key,
#required this.iconPath,
this.placeHolder = "",
this.onFocusChange
})
: super(key: key);
Usage:
void _focusChanged(bool value) {
// using null-aware operator (for both with and without null safety)
onFocusChange?.call(value);
// or without null-aware operator
// with null safety
if(onFocusChange != null) {
onFocusChange!(value);
}
// without null safety
if(onFocusChange != null) {
onFocusChange(value);
}
}
Dart 2.17 update:
Although it often makes sense to place positional arguments first, named arguments can be placed anywhere in the argument list when it suits your API:
repeat(times: 2, () {
...
});
Have a look at Optional Parameters to understand better.
Edit: Thank you Jonah Williams to clarification.
You can use a default value that does nothing:
class TextInputWithIcon extends StatefulWidget {
final String iconPath;
final String placeHolder;
final Function(bool) onFocusChange;
const TextInputWithIcon(
{Key key,
#required this.iconPath,
this.placeHolder = "",
this.onFocusChange = _dummyOnFocusChange})
: assert(onFocusChange != null), super(key: key);
#override
_TextInputWithIconState createState() => _TextInputWithIconState();
static dynamic _dummyOnFocusChange(bool val) {}
}
I created a static named function instead of just a closure as a default value because closures are not const and currently default values need to be const.
I added the assert(...) to ensure that an error is shown when null is passed explicitly.
Another option if you don't like named parameters (like me :/) is:
function_name (argument1, [argument2]) {
// statements
}
arguments in brackets are optional.
source