use_ key_ in_ widget_ constructors
Constructors for public widgets should have a named 'key' parameter.
Description
#
The analyzer produces this diagnostic when a constructor in a subclass of
Widget
that isn't private to its library doesn't have a parameter named
key
.
Example
#
The following code produces this diagnostic because the constructor for
the class
MyWidget
doesn't have a parameter named
key
:
import 'package:flutter/material.dart';
class MyWidget extends StatelessWidget {
MyWidget({required int height});
}
The following code produces this diagnostic because the default
constructor for the class
MyWidget
doesn't have a parameter named
key
:
import 'package:flutter/material.dart';
class MyWidget extends StatelessWidget {}
Common fixes
#
Add a parameter named
key
to the constructor, explicitly declaring the
constructor if necessary:
import 'package:flutter/material.dart';
class MyWidget extends StatelessWidget {
MyWidget({super.key, required int height});
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.