Flutter 单例写法 2023-08-04周五

2023-08-03  本文已影响0人  勇往直前888

简介

Flutter单例有好几种写法;
可以沿用Swift的写法;也可以用factory工厂方法;

静态变量

Intercom插件采用的直接用静态变量的方法感觉很不错,以后就这么写。

class Intercom {
  /// private constructor to not allow the object creation from outside.
  Intercom._();

  static final Intercom _instance = Intercom._();

  /// get the instance of the [Intercom].
  static Intercom get instance => _instance;

  /// ……

}
上一篇 下一篇

猜你喜欢

热点阅读