Reputation: 802
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);
}
Upvotes: 31
Views: 16507
Reputation: 334
I guess it'd be better to merge the two answers that you already received.
@mmcdon20 is right, you don't have to pass any argument to the state constructor because, as @Mohamed stated, you can do that via the init_state()
using the widget argument or in the _OverviewPageState
's constructor.
(I am just doing the fusion of the answers here to be more precise for the ones who are new to Flutter)
The final result should look like this:
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>{
late int idCopy;
// the 'late' keyword is necessary because otherwise Flutter would expect
// an initialization of the value from a Constructor that accept 'int id'
// as a parameter which is what we are trying to avoid because it's not the
// correct path to follow when you initialize a State of StatefulWidget
@override
void initState() {
super.initState();
idCopy = widget.id;
//your code here
}
}
Hope it clarify doubts for the newcomers :)
Credits: @Mohamed_Hammane, @mmcdon20
Upvotes: 13
Reputation: 2803
Looking at the discussion above, the question revolves around the point that why is wrong to pass arguments inside createState
.
One thing which is clear is that the main reason for avoiding logic in createState
is that it is called only once when the widget is first created. If you have any logic that depends on the widget's state or context
, it may not behave as expected when the widget is reused or rebuilt.
For example, let's say you have a StatefulWidget
that fetches data from an API in its createState
method
. If the widget is rebuilt because of a state change or navigation, the API call will not be made again because the createState method is not called again. This can result in stale or incorrect data being displayed.
Passing parameters to createState
is not necessarily an issue. However, the avoid_logic_in_create_state
lint rule suggests that you should avoid putting any logic in createState
method for the reasons I mentioned earlier. To make it more holistic, virtually anything inside createState is not recommended and linter
throws warning. Its important to note the linter works by validating a logic and as long as you do something in createState, that violates the logic.
Though it would be nice if flutter linter can get an upgrade and add this part.
Upvotes: 1
Reputation: 151
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
}
Upvotes: 10
Reputation: 6736
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
}
Upvotes: 50