Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: @required keyword

I don't really understand how required works. For example I've seen this code:

class Test{
  final String x;
  Test({
    required this.x
  });

  factory Test.initial(){
    return Test(x: "");
  }
}

But what should required do here? Seems like it makes an optional parameter a non optional parameter.

like image 773
Little Monkey Avatar asked Jan 14 '19 12:01

Little Monkey


People also ask

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 is the difference between @required and required in flutter?

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.

Is Dart a keyword?

Unlike Java, Dart doesn't have the keywords public , protected , and private .

What is final keyword in flutter?

The final keyword is used to hardcode the values of the variable and it cannot be altered in future, neither any kind of operations performed on these variables can alter its value (state).


3 Answers

Update

As of Dart 2.12, the required keyword replaces the @required meta annotation. For detailed info look into the official FAQ. The following answer has been updated to reflect both this and null safety.

Parameters required by default

The parameters of a class constructor or function are required by default.

class Test {
  final String x;
  Test(this.x);
}

You're not allowed to do this:

final value = Test(); 
// 1 positional argument(s) expected, but 0 found.

You must do this:

final value = Test('hello');

Optional named parameters

If you surround a parameter with curly braces, though, in addition to becoming a named parameter, it also becomes optional.

Since it's optional, the property must either be nullable like this:

class Test {
  final String? x;
  Test({this.x});
}

Or it has to have a default value like this:

class Test {
  final String? x;
  Test({this.x = ''});
}

So now this is ok:

final value = Test(); 

And so is this:

final value = Test(x: 'hello'); 

Required named parameters

Sometimes you don't want to allow a parameter to be null and there is no natural default variable. In that case you can add the required keyword in front of the parameter name:

class Test {
  final String x;
  Test({required this.x});
}

This is not ok anymore:

final value = Test(); 
// The named parameter 'x' is required, but there's no corresponding argument.

But this is still fine:

final value = Test(x: 'hello');
like image 88
Suragch Avatar answered Sep 18 '22 13:09

Suragch


Dart 2.12 (null safety):

Beginning with Dart 2.12, the @required annotation is now replaced by the required keyword. You should mark your field required if it is mandatory for others to pass some value to it.

For example:

class Foo {
  final int a; // Mandatory? Use 'required'
  final int b; // Not mandatory? Don't use 'required'

  Foo({
    required this.a, // Marked 'required'
    this.b = 1, 
  });
}

Usage:

Foo(); // Error: 'a' is required
Foo(a: 0); // Good
Foo(a: 0, b: 1); // Good
like image 32
CopsOnRoad Avatar answered Sep 17 '22 13:09

CopsOnRoad


@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. It will not create compile errors, at least for what I know.

like image 32
Martyns Avatar answered Sep 19 '22 13:09

Martyns