Don't put any logic in createState after installing flutter_lints in Flutter - 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
}

Related

How to set default value of function in a constructor (Flutter)

I want to set default function in my child Widget in a constructor.
Basically, I have two widgets
Login (Parent Widget)
AppButton (Child Widget)
Here is my AppButton.dart
And I am calling this child widget in Login.dart (Parent) like this:
AppButton(title: "Login")
Please give me a way that to set default function without making "onPress" required for it's Parent (Login.dart)
TIA
Only static value can be set as default value in constructor, so you need define you function as static like this:
class AppButton extends StatefulWidget {
final Function onPress;
const AppButton({Key? key, this.onPress = _onPress}) : super(key: key);
static void _onPress(){}
#override
State<AppButton> createState() => _AppButtonState();
}
just make it nullable:
class MyButton extends StatefulWidget {
final void Function()? onPress;
final String title;
const MyButton({Key? key, this.onPress, required this.title}) : super(key: key);
#override
State<MyButton> createState() => _MyButtonState();
}
class _MyButtonState extends State<MyButton> {
void Function() defaultOnPress = (){
// your default function here
};
#override
Widget build(BuildContext context) {
return ElevatedButton(onPressed: widget.onPress ?? defaultOnPress, child: const Text("my button"));
}
}
still you can get const constructor
you could put "static" before childOnPress()

"Avoid using private types in public APIs" Warning in Flutter

I have been working on a flutter project and I have noticed Avoid using private types in public APIs.
Is there a way to fix this warning?
class SubCategoriesPage extends StatefulWidget {
final MainModel mainModel;
// final Ads ad;
const SubCategoriesPage(this.mainModel, {Key? key}) : super(key: key);
#override
_SubCategoriesPage createState() { // Avoid using private types in public APIs.
return _SubCategoriesPage();
}
}
Because createState method return State<Example> so it's preventing returning any private State.
You need to update your code like this.
class SubCategoriesPage extends StatefulWidget {
final MainModel mainModel;
// final Ads ad;
const SubCategoriesPage(this.mainModel, {Key? key}) : super(key: key);
#override
State<SubCategoriesPage> createState() { // Avoid using private types in public APIs.
return _SubCategoriesPage();
}
}
Since this is a StatefulWidget, I'm guessing the _SubCategoriesPage class inherits from State, since it's being returned by createState().
If so, the return type can be changed to State. Since State is public, it can safely be returned from the public createState() method.
Just change _SubCategoriesPage by State
For those using Riverpod, change _SubCategoriesPage by ConsumerState
I had the same issue using this code
class MyPage extends StatefulWidget {
const MyPage({super.key});
#override
_MyPageState createState() => _MyPageState();
}
I tried using this method - State createState() { // Avoid using private types in public APIs.
return _MyPageState();
but than I got this error message - The name MyPageState isn't a type so it can't be used a type argument.

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

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

Pass to Widget a Function that returns a future

I want to pass to my Widget a function that returns a future:
class CircularButtonWithIcon extends StatefulWidget {
CircularButtonWithIcon(
{Key key,
#required this.onPress,
this.activeStatus})
: super(key: key);
final Function activeStatus;
class _CircularButtonWithIconState extends State<CircularButtonWithIcon> {
bool active;
#override
void initState() {
super.initState();
widget.activeStatus.then(...);
}
However Dart's class Function has no way to specify that the function's return type.
Is it possible to do such thing?
You can add the return type front of the Function
i.e.
class CircularButtonWithIcon extends StatefulWidget {
Future<void> Function() activeStatus;
CircularButtonWithIcon({Key key, #required this.onPress, this.activeStatus,}) : super(key: key);
}

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