use-key-in-widget-constructors
Same as use_key_in_widget_constructors in Dart lints
- Group
- Correctness
- Path
lint/correctness/use-key-in-widget-constructors- Default
- On (recommended)
- Origin
- Dart lints
- Domain
- flutter
- Rule
- 494 of 512
Require public widget constructors to accept or initialize a Flutter Key.
Primary headers supply parameters; their optional this bodies supply
constructor annotations and initializers.
Diagnostics underline the named constructor identifier, or the type name
for unnamed constructors, rather than modifiers or the constructor body.
Invalid
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
class MissingConstructor extends StatelessWidget {}
class MissingKey extends StatelessWidget {
const MissingKey();
}
class WrongTypedKey extends StatelessWidget {
const WrongTypedKey({String? key});
}
class MissingConstructor extends StatelessWidget {}class MissingKey extends StatelessWidget { const MissingKey();class WrongTypedKey extends StatelessWidget { const WrongTypedKey({String? key});Valid
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
class SuperKey extends StatelessWidget {
const SuperKey({super.key});
}
class ForwardedKey extends StatelessWidget {
const ForwardedKey({Key? key}) : super(key: key);
}
class CoalescedKey extends StatelessWidget {
const CoalescedKey({Key? key}) : super(key: key ?? const Key('fallback'));
}
class HardCodedKey extends StatelessWidget {
const HardCodedKey() : super(key: const Key('fixed'));
}
class RedirectedKey extends StatelessWidget {
const RedirectedKey({Key? key}) : this.named(key: key ?? const Key('fallback'));
const RedirectedKey.named({Key? key}) : super(key: key);
}
class _PrivateWidget extends StatelessWidget {}
class PrivateConstructor extends StatelessWidget {
const PrivateConstructor._();
}
class FactoryWidget extends StatelessWidget {
factory FactoryWidget() => throw UnimplementedError();
}
// Supplying the key argument is sufficient even when its type is invalid.
class WrongForwardingType extends StatelessWidget {
const WrongForwardingType({required String value}) : super(key: value);
}
@visibleForTesting
class TestWidget extends StatelessWidget {}
abstract class WidgetInterface implements Widget {}
abstract class WidgetInterfaceChild extends WidgetInterface {}
abstract class ConstructedWidgetInterface implements Widget {
ConstructedWidgetInterface();
}Configure use-key-in-widget-constructors
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"use-key-in-widget-constructors": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/use-key-in-widget-constructors: <reason>More correctness rules
- use-once-constructors-once-provider
use_once_constructors_once_providerflutterRequire Riverpod once-providers to be created through
.once(). - use-ref-and-state-synchronouslyrecommendedriverpod
Flags notifier ref and state accesses after an async gap without ref.mounted.
- use-ref-read-synchronouslyrecommendedriverpod
Flags Riverpod WidgetRef.read calls after an async gap without a mounted guard.
- use-setstate-synchronouslyrecommendedflutter
Require a mounted check before calling setState after an async gap.
- valid-regexps
valid_regexpsrecommendedDisallow statically known invalid
dart:coreregular expressions. - void-checks
void_checksrecommendedDon't pass or assign values to
void(orFutureOr<void>) locations. - add-equatable-propsrecommendedequatable
Flags concrete Equatable subclasses that do not declare an instance props property.
- always-pass-global-keyflutter
Keep
GlobalKeysstable and pass owned State fields to widgets.