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

4,209

Solution 1

@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.

Solution 2

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.

Share:
4,209
ulukbek
Author by

ulukbek

Updated on December 29, 2022

Comments

  • ulukbek
    ulukbek over 1 year

    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,
              ),
            ),
          ),
        );
      }
    }