no-logic-in-create-state

Same as no_logic_in_create_state in Dart lints

Path
lint/correctness/no-logic-in-create-state
Default
On (recommended)
Origin
Dart lints
Domain
flutter
Rule
291 of 512
recommendedflutter

Require StatefulWidget.createState to return only a fresh zero-argument Flutter State construction.

Widget and State identities are resolved canonically, including prefixes, aliases, and intermediate project subclasses. Unresolved ancestry or constructor identity is not guessed from a name suffix.

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

class _W1State extends State<W1> {}
class _W2State extends State<W2> { void init() {} }
class _W3State extends State<W3> {}
class _W4State extends State<W4> { _W4State(Object value); }
class _W5State extends State<W5> { int counter = 0; }

// A fully known hierarchy proves that the constructed type is not a State.
class W6 extends StatefulWidget {
  @override
  State<W6> createState() => _NotAState();
}

class W7 extends StatefulWidget {
  @override
  State<W7> createState() {
    return new _NotAState();
  }
}

class _NonStateBase {}
class _NotAState extends _NonStateBase {}
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.
27 State<W4> createState() =>
28 _W4State(this);
Avoid logic in `createState`; it should only return a new `State` instance.
33 State<W5> createState() {
34 return _W5State()..counter = 0;
Avoid logic in `createState`; it should only return a new `State` instance.
46 @override
47 State<W6> createState() => _NotAState();
Avoid logic in `createState`; it should only return a new `State` instance.
52 State<W7> createState() {
53 return new _NotAState();

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

class _G1State extends State<G1> {}
class _G2State extends State<G2> {}
class _G5State extends State<G5> {}

// The constructor resolves, but its missing ancestor might be a State.
class G6 extends StatefulWidget {
  @override
  State<G6> createState() => _UnresolvedState();
}

class G7 extends StatefulWidget {
  @override
  State<G7> createState() {
    return new _UnresolvedState();
  }
}

class _UnresolvedState extends MissingStateBase {}

Configure no-logic-in-create-state

Set its severity in falcon.json. See the configuration guide.

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

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/no-logic-in-create-state: <reason>
  • no-self-assignmentsno_self_assignmentsrecommended

    Flags assignments of a variable to itself, such as x = x;.

  • null-check-on-nullable-type-parameternull_check_on_nullable_type_parameterrecommended

    Flags a null assertion x! on a nullable type parameter T? whose expected type is T or T? for a potentially nullable T, and null-assert patterns whose matched value is a nullable type parameter.

  • null-closuresnull_closuresrecommended

    Reject literal null in the closure slots of curated SDK APIs.

  • prefer-correct-json-casts

    Decode JSON containers using their actual runtime generic types.

  • prefer-define-hero-tagflutter

    Require an explicit heroTag on Flutter FloatingActionButton constructors.

  • prefer-expect-latertest

    Requires expectLater when a canonical expect call receives an unawaited Future.

  • prefer-immutable-bloc-eventsrecommendedbloc

    Flags Bloc event classes without an explicit @immutable annotation.

  • prefer-immutable-bloc-staterecommendedbloc

    Flags Bloc state classes without an explicit @immutable annotation.