Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,
          ),
        ),
      ),
    );
  }
}
like image 846
ulukbek Avatar asked May 21 '21 18:05

ulukbek


People also ask

What is the difference between @required and required in flutter?

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.

What is @required in flutter?

@required is an annotation that will create a warning for you to remember that the named parameter is necessary for the class to work as expected.

What are required parameters in DART?

Dart required positional parametersWe declare required positional parameters with a type and name, e.g., int: a . The name is for reference in the function, not at the call site. You specify only the value without a parameter name when calling a function. int sum(int a, int b) { // 1.


2 Answers

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

like image 176
Jose Georges Avatar answered Oct 16 '22 16:10

Jose Georges


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.

like image 7
mLstudent33 Avatar answered Oct 16 '22 16:10

mLstudent33