【开发经验】在Flutter中使用dart的单例模式
最近学Flutter,百度了一下午也没搜到Dart语言的单例怎么写,最后去看一个NotificationCenter的库的源码,才找到单例写法,经测试可用,贴一下代码。
Flutter中,dart的单例模式设计
创建一个单例的Manager类
class Manager { // 工厂模式 factory Manager() =>_getInstance() static Manager get instance => _getInstance(); static Manager _instance; Manager._internal() { // 初始化 } static Manager _getInstance() { if (_instance == null) { _instance = new Manager._internal(); } return _instance; } }
调用
// 无论如何初始化,取到的都是同一个对象 Manager manager = new Manager(); Manager manager2 = Manager.instance;
相关推荐
RedCode 2020-10-28
万物weiyi 2020-06-16
Samlss 2020-06-04
liutong 2020-05-12
万物weiyi 2020-03-04
mryangjx 2020-03-01
liutong 2020-02-26
liutong 2020-02-22
Samlss 2020-02-22
chaoxiao 2020-02-15
mryangjx 2020-01-28
mryangjx 2020-01-25
liutong 2020-01-12
apowerfulman 2020-01-07
Samlss 2020-01-07
万物weiyi 2020-01-01
mryangjx 2019-12-25
Samlss 2019-12-15