Counts the Clouds

Flutterのnull-safety以前のWidgetコンストラクタをnull-safetyにする
2021.08.11

Flutter
Dart

The parameter ‘key’ can’t have a value of ‘null’ because of its type, but the implicit default value is ‘null’.

null-safety以前のバージョンのサンプルなどをnull-safetyなプロジェクトに持ってくるとよく遭遇する。

class _MyWidget extends StatefulWidget {
  _MyWidget({
    Key key, // <- エラー
    @required this.child, // <- エラー
  }) : super(key: key);

  final Widget child;

  // ...
}
The parameter 'key' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required'

Nullable型Key?keyを受け取るようにする。

@requiredアノテーションはrequiredアノテーションにする。

class _MyWidget extends StatefulWidget {
  _MyWidget({
    Key? key,
    required this.child,
  }) : super(key: key);

  final Widget child;

  // ...
}