no-logic-in-create-state
Same as no_logic_in_create_state in Dart lints
- Group
- Correctness
- Path
lint/correctness/no-logic-in-create-state- Default
- On (recommended)
- Origin
- Dart lints
- Domain
- flutter
- Rule
- 291 of 512
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
// 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 {} @override State<W1> createState() { @override State<W2> createState() => _W2State()..init(); @override State<W3> createState() { State<W4> createState() => _W4State(this); State<W5> createState() { return _W5State()..counter = 0; @override State<W6> createState() => _NotAState(); State<W7> createState() { return new _NotAState();Valid
// 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.
{
"linter": {
"rules": {
"correctness": {
"no-logic-in-create-state": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/no-logic-in-create-state: <reason>More correctness rules
- no-self-assignments
no_self_assignmentsrecommendedFlags assignments of a variable to itself, such as
x = x;. - null-check-on-nullable-type-parameter
null_check_on_nullable_type_parameterrecommendedFlags a null assertion
x!on a nullable type parameterT?whose expected type isTorT?for a potentially nullableT, and null-assert patterns whose matched value is a nullable type parameter. - null-closures
null_closuresrecommendedReject literal
nullin 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
FloatingActionButtonconstructors. - 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.