Skip to main content

no_logic_in_create_state

Don't put any logic in 'createState'.

Description

#

The analyzer produces this diagnostic when an implementation of createState in a subclass of StatefulWidget contains any logic other than the return of the result of invoking a zero argument constructor.

Examples

#

The following code produces this diagnostic because the constructor invocation has arguments:

dart
import 'package:flutter/material.dart';

class MyWidget extends StatefulWidget {
  @override
  MyState createState() => MyState(0);
}

class MyState extends State {
  int x;

  MyState(this.x);
}

Common fixes

#

Rewrite the code so that createState doesn't contain any logic:

dart
import 'package:flutter/material.dart';

class MyWidget extends StatefulWidget {
  @override
  MyState createState() => MyState();
}

class MyState extends State {
  int x = 0;

  MyState();
}