what is the difference between required and @required in flutter. What is the difference between them and when do we need to use them?
Asked Answered
P

2

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,
          ),
        ),
      ),
    );
  }
}
Puffer answered 21/5, 2021 at 18:10 Comment(1)
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.Cerebrovascular
P
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.

Peripheral answered 21/5, 2021 at 18:45 Comment(2)
so required is stronger than @required?Tarpaulin
Yes, required is mandatory, whereas @required isn'tPeripheral
T
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.

Tarpaulin answered 24/8, 2021 at 1:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.