The instance member 'key' can't be accessed in an initializer - flutter

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);
}

Related

How can I use widget parameters as variables?

I cant use the parameter variable.
class HomePage extends StatefulWidget {
const HomePage({
Key? key,
required this.userState,
required this.currentPageForRoute,
}) : super(key: key);
final bool userState;
final int currentPageForRoute;
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int currentPage = widget.currentPageForRoute;
//And then, i going to change currentPage often and use it.
}
I want use widget.currentPageForRoute as currentPage variable, but i cant.
(If I can use it, I will change it often.)
The error massage: Undefined name 'widget'.
You can use initState method or late
late int currentPage = widget.currentPageForRoute;

Don't put any logic in createState after installing flutter_lints in Flutter

I installed flutter_lints plugin in my project, after installing then it shows a warning message "Don't put any logic in createState". How to solve this issue?
class OverviewPage extends StatefulWidget {
final int id;
const OverviewPage({Key? key, required this.id}) : super(key: key);
#override
_OverviewPageState createState() => _OverviewPageState(id); // Warning on this line
}
class _OverviewPageState extends State<OverviewPage>{
late final int id;
_OverviewPageState(this.id);
}
Don't pass anything to _OverviewPageState in the constructor.
class OverviewPage extends StatefulWidget {
final int id;
const OverviewPage({Key? key, required this.id}) : super(key: key);
#override
_OverviewPageState createState() => _OverviewPageState();
}
class _OverviewPageState extends State<OverviewPage>{
// if you need to reference id, do it by calling widget.id
}
I someone want to initiate a variable inside the state from the main class you can use for example, cause you can't use it in constructor class.
#override
void initState() {
super.initState();
id = widget.id;
code = widget.code;
//your code here
}

Flutter State<WidgetName> createState() => WidgetNameState()

In Flutter, when initializing a new stateful widget, it is being initialized like this by default:
class WidgetName extends StatefulWidget {
const WidgetName({ Key? key }) : super(key: key);
#override
WidgetNameState createState() => WidgetNameState();
}
I have seen another way of initializing the statefulwidget with #override being slightly different.
class WidgetName extends StatefulWidget {
const WidgetName({ Key? key }) : super(key: key);
#override
State<WidgetName> createState() => WidgetNameState();
}
Notice in the #override method, WidgetNameState became State<WidgetName>. There is an explanation in the Flutter repo that explains it: Link, but I couldn't comprehend what it is trying to say.
What does State<WidgetName> do exactly? Does it give any advantages?
I thought it wouldn't be necessary as WidgetNameState already extends from State<WidgetName> in its class construction.
class WidgetNameState extends State<WidgetName> {
#override
Widget build(BuildContext context) {
Using the generic really let's you define an abstract Widget interface that must be used without having to define any state along with that abstract widget interface.
Let's look at using the concrete class first (WidgetNameState). This is an abstract definition, we must define the state if we do this.
abstract class FooWidget extends StatefulWidget {
const FooWidget({Key? key}) : super(key: key);
#override
_FooWidgetState createState();
}
abstract class _FooWidgetState extends State<FooWidget> {
#override
Widget build(BuildContext context) {
return Container();
}
}
Now to be able to extend this you must extend both the widget and the state.
class ImplementedFooWidget extends FooWidget {
const ImplementedFooWidget({Key? key}) : super(key: key);
#override
_ImplementedFooWidgetState createState() => _ImplementedFooWidgetState();
}
class _ImplementedFooWidgetState extends _FooWidgetState {
#override
Widget build(BuildContext context) {
return Container();
}
}
Now let's look at using the generic (State<WidgetName>). If we use the generic, we can just define and extend the widget and have our own custom state.
abstract class BarWidget extends StatefulWidget {
const BarWidget({Key? key, required this.someRequiredString})
: super(key: key);
final String someRequiredString;
#override
State<BarWidget> createState();
}
class ImplementedBarWidget extends BarWidget {
const ImplementedBarWidget({Key? key, required String someRequiredString})
: super(
key: key,
someRequiredString: someRequiredString,
);
#override
_ImplementedBarWidgetState createState() => _ImplementedBarWidgetState();
}
class _ImplementedBarWidgetState extends State<ImplementedBarWidget> {
#override
Widget build(BuildContext context) {
return Container();
}
}
I hope that all makes sense.

How to receive a parameter and use in initState within a stateful widget

I have a stateful widget that has one method called in initialisation. I wanna know how to be able to get a parameter from the previous screen and pass it in initState to my initialisation method
class LabDetalheWidget extends StatefulWidget {
final String path;
const LabDetalheWidget({
Key key,
this.path,
}) : super(key: key);
You can pass parameter like that
class MyWidget extends StatefulWidget {
final String param;
const MyWidget({
Key key,
this.param,
}) : super(key: key);
#override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
#override
void initState() {
print(widget.param);
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
);
}
}
Inside the state you can access the parameter like that
print(widget.param)
I believe you wanna pass data across routes.. If so then read this flutter cookbook's section you might get the idea
https://flutter.dev/docs/cookbook/navigation/passing-data

createState mathod is not working in StatefulWidget in flutter.... it shows the following error

there createState mehod is not working..
[class MysignupPage extends StatefulWidget {
const MysignupPage({ Key key }) : super(key: key);
#override
_MysignupPageState createState() => _MysignupPageState();
}
class _MysignupPageState extends StatelessWidget {
final _auth = FirebaseAuth.instance;][1]
this error is shown
[1]: https://i.stack.imgur.com/An0iV.png