31

If I delete required from the named parameters, it gives me an error:

The parameter 'color' // can't have a value of 'null' because of its type, but the implicit default value is 'null'.

What is the difference between them and when do we need to use them?

class RoundedButton extends StatelessWidget {
  late final Color color;
  final String title;
  final VoidCallback? onPressedInput;

  RoundedButton(
      {required this.color,
      required this.title,
      @required this.onPressedInput});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        color: color,
        borderRadius: BorderRadius.circular(30.0),
        elevation: 5.0,
        child: MaterialButton(
          onPressed: onPressedInput,
          minWidth: 200.0,
          height: 42.0,
          child: Text(
            title,
          ),
        ),
      ),
    );
  }
}
1
  • 7
    As of Dart 2.12/Flutter 2.0, you shouldn't ever need to use @required again, and should instead always use required when you have a non-nullable named parameter.
    – Abion47
    May 21, 2021 at 22:30

2 Answers 2

42

@required is just an annotation that allows analyzers let you know that you're missing a named parameter and that's it. so you can still compile the application and possibly get an exception if this named param was not passed.

However sound null-safety was added to dart, and required is now a keyword that needs to be passed to a named parameter so that it doesn't let the compiler run if this parameter has not been passed. It makes your code more strict and safe.

If you truly think this variable can be null then you would change the type by adding a ? after it so that the required keyword is not needed, or you can add a default value to the parameter.

2
  • so required is stronger than @required? Aug 24, 2021 at 1:14
  • Yes, required is mandatory, whereas @required isn't Aug 24, 2021 at 18:30
7

https://dart.dev/null-safety/faq#how-does-required-compare-to-the-new-required-keyword

The @required annotation marks named arguments that must be passed; if not, the analyzer reports a hint.

With null safety, a named argument with a non-nullable type must either have a default or be marked with the new required keyword. Otherwise, it wouldn’t make sense for it to be non-nullable, because it would default to null when not passed.

When null safe code is called from legacy code the required keyword is treated exactly like the @required annotation: failure to supply the argument will cause an analyzer hint.

When null safe code is called from null safe code, failing to supply a required argument is an error.

What does this mean for migration? Be careful if adding required where there was no @required before. Any callers not passing the newly-required argument will no longer compile. Instead, you could add a default or make the argument type nullable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.