Flutter: @required keyword

做~自己de王妃 提交于 2019-12-06 00:04:26

@required is needed when you have more than 1 named parameters and you want some of the parameters to be mandatory, you annotate it using @required.

Example

class Test {
  final String a; // say a is mandatory
  final String b;
  final String c;
  final String d;

  Test({
    @required this.a, // annotate it using required
    this.b,
    this.c,
    this.d,
  });
}

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!