当你用 {super.key} 声明一个常量构造函数时,这意味着什么?

Ter*_*ice 0 constructor dart flutter

我有以下基本代码

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'),
          ),
        ));
  }
}
Run Code Online (Sandbox Code Playgroud)

注意常量构造函数,

const MyApp({super.key});
Run Code Online (Sandbox Code Playgroud)

我认为卷曲提示 {} 用于可选的命名参数。

我在这里看到没有名字。

这是怎么回事?将 {super.key} 传入常量构造函数意味着什么?

Wil*_*ran 5

从 Dart 2.17 开始添加了一个新的Super 初始化器。请参阅下图了解更多信息。

事实上,当我们将新功能应用到 Flutter 框架中时,我们发现总共减少了近两千行代码!

在此输入图像描述

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