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

Time:12-23

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

CodePudding user response:

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
}

CodePudding user response:

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>{
 

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
   id = widget.bloc.id; // or find the way to find id 
  }
}
  • Related