Flutter如何将变量设为私有并在构造函数中使用@required对其进行初始化 [英] Flutter how to make a variable private and initialize it with @required in constructor

查看:249
本文介绍了Flutter如何将变量设为私有并在构造函数中使用@required对其进行初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使该字段私有化并在课程中用 @required 进行注释?

How can I make the field private and annotate it with @required in a class?

例如,这段代码给我错误:

For example this code gives me error:

class AuthenticationService {
  final Api _api;

  AuthenticationService({@required this._api}); // error in this line
}


推荐答案

class AuthenticationService {
  final Api _api;

  AuthenticationService({@required Api api}) : _api = api;
}

这篇关于Flutter如何将变量设为私有并在构造函数中使用@required对其进行初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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