Flutter:@required关键字 [英] Flutter: @required keyword

查看:809
本文介绍了Flutter:@required关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解 @required 的工作方式。例如,我看过以下代码:

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: "");
  }

}

但是<$ c $应该是什么c> @required 在这里做什么?好像它使可选参数成为非可选参数。

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

推荐答案

@required ,您可以使用 @required 对其进行注释。

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

示例

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

这篇关于Flutter:@required关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆