Skip to main content

Flutter Constructors for public widgets should have a named 'key' parameter Error Fix

Today when I was writing code for my flutter app then accidently I forgot to add KEY constructor to our Root class which calls in the Void Main() function. Root class is our main class which extends StatelessWidget and within it child class with StatefulWidget. I have seen this error as code improvement suggestion in Visual studio code editor. If you want to read more deeply about this error then you can read it from Flutter Dart official documentation

Error Description:
Constructors for public widgets should have a named 'key' parameter.
Try adding a named parameter to the constructor.dartuse_key_in_widget_constructors
class MyApp extends StatelessWidget
package:myapp/main.dart

Error Screenshot:

Flutter Constructors for public widgets should have a named 'key' parameter Error Fix

Flutter Constructors for public widgets should have a named 'key' parameter Error Fix:

1. To fix this error all you have to do is use Const YourClassName then Super.Key format. See the below code example.
const MyApp({super.key});
2. Here MyApp is called inside void main function of our flutter app.
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: SafeArea(
              child: Center(
            child: ChildWidget(),
          ))),
    );
  }
}

Comments