Rules / Correctness / no-logic-in-create-state

no-logic-in-create-state

lint/correctness/no-logic-in-create-state
recommendedflutter
Same as no_logic_in_create_state · Dart lints

Require createState to do nothing but return a new State instance.

Flags a StatefulWidget's createState whose body is anything other than a bare zero-argument construction of its State — that is, => _MyState() or { return _MyState(); }. Flutter may call createState more than once over a widget's lifetime and does not promise when, so initialization, argument passing, or side effects placed here run at unpredictable moments and can leak data between distinct State objects. Do that work in State.initState or in the State's field initializers instead, and pass configuration through the widget rather than the constructor call.

Invalid

example.dartdart
// Bad: createState contains logic beyond returning a new State instance.
import 'package:flutter/material.dart';

class W1 extends StatefulWidget {
  @override
  State<W1> createState() {
    print('creating');
    return _W1State();
  }
}

class W2 extends StatefulWidget {
  @override
  State<W2> createState() => _W2State()..init();
}

class W3 extends StatefulWidget {
  @override
  State<W3> createState() {
    final state = _W3State();
    return state;
  }
}

class W4 extends StatefulWidget {
  @override
  State<W4> createState() => _W4State(this);
}

class W5 extends StatefulWidget {
  @override
  State<W5> createState() {
    return _W5State()..counter = 0;
  }
}
Avoid logic in createState; it should only return a new State instance.
5 @override
6 State<W1> createState() {
Avoid logic in createState; it should only return a new State instance.
13 @override
14 State<W2> createState() => _W2State()..init();
Avoid logic in createState; it should only return a new State instance.
18 @override
19 State<W3> createState() {
Avoid logic in createState; it should only return a new State instance.
26 @override
27 State<W4> createState() => _W4State(this);
Avoid logic in createState; it should only return a new State instance.
31 @override
32 State<W5> createState() {

Valid

example.dartdart
// Good: createState only returns a new State instance (or the class is not a StatefulWidget).
import 'package:flutter/material.dart';

class G1 extends StatefulWidget {
  @override
  State<G1> createState() => _G1State();
}

class G2 extends StatefulWidget {
  @override
  State<G2> createState() {
    return _G2State();
  }
}

class G3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final label = compute();
    return Text(label);
  }
}

class G4 {
  int createState() {
    final value = compute();
    return value;
  }
}

class G5 extends StatefulWidget {
  @override
  State<G5> createState() => _G5State();
}

How to configure

Set the severity of no-logic-in-create-state in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "no-logic-in-create-state": "error"
      }
    }
  }
}