Fix Flutter error: Required isn't type

Null safety is a great feature for any programming language. Dart is not any different. Dart introduced it in Version 2.0. You can read more about Understanding Null Safety in Dart language documentation.

When you create a new Flutter application in Visual Studio Code, null safety is not enabled by default. You will observe this when you create a Widget with constructor that has a required parameter. There is a walk through in Flutter document that has a Widget with such constructor. Following code is such an example.

class TopBar extends StatelessWidget {
  TopBar({required this.title});

  final Widget title;
  ... removed rest of code for brevity...

Above code will result in following errors.

required isn't a type.
Try correcting the name to match an existing type.

The parameter type 'dynamic' is incompatible with the field type 'Widget'.
Try changing or removing the parameter's type, or changing the field's type.

To fix these errors, you will need to enable null safety. Open pubspec.yaml file in your project. You will find following setting in that file.

environment:
  sdk: ">=2.7.0 <3.0.0"

In Dart language documentation, you will find statement Sound null safety is available in Dart 2.12 and Flutter 2. You can change setting in pubspec.yaml file to use Dart 2.12 and not 2.7. Following is updated file.

environment:
  sdk: ">=2.12.0 <3.0.0"

Search

Social

Weather

0.1 °C / 32.1 °F

weather conditions Clouds

Monthly Posts

Blog Tags