Home > Software engineering >  What does it mean when you declare a constant constructor with {super.key}?
What does it mean when you declare a constant constructor with {super.key}?

Time:07-22

I have the following bare bones code

import 'package:flutter/material.dart';

//write the main method
void main() => runApp(const MyApp());

//write the MyApp class that extends the StatelessWidget class
class MyApp extends StatelessWidget {
  //write the const ctor
  const MyApp({super.key});
  //write the build method
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'hello',
        home: Scaffold(
          appBar: AppBar(
            title: const Text('hello'),
          ),
          body: const Center(
            child: Text('hello'),
          ),
        ));
  }
}

Note the constant constructor,

const MyApp({super.key});

I thought the curly cues {} were used for optional named parameters.

I see no name here.

What's going on? What does it mean to pass in {super.key} into a constant constructor?

CodePudding user response:

What you probably know is this version:

const MyApp({this.key});

Which normally means you scoll up a bit and see a line like

final Key key;

This is called enter image description here

Reference: https://medium.com/dartlang/dart-2-17-b216bfc80c5d

  • Related