flutterdart

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

Solution

  • 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
    }