Flutter required keyword

33,388

Solution 1

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

Solution 2

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');

Solution 3

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

Solution 4

@required bounds you to pass @required marked arguments while creating object of Class. For example, while showing a dialog, you'd mark context as required since, you cannot show dialog without having a valid context. But, you should not overuse it.

Share:
33,388
Little Monkey
Author by

Little Monkey

I'm just a little monkey!

Updated on July 05, 2022

Comments

  • Little Monkey
    Little Monkey almost 2 years

    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.