单例写法
class Singleton{
Singleton._privateConstructor();
static final Singleton _instance = Singleton._privateConstructor();
static Singleton get instance { return _instance;}
}
Dio单例
class DioManager {
//写一个单例
DioManager._privateConstructor();
static Dio _dio = new Dio();
static DioManager? _instance;
static DioManager? getInstance() {
if (_instance == null) {
_instance = DioManager._privateConstructor();
}
_instance!._init();
return _instance;
}
_init() {
_dio.options.baseUrl = Constant.baseUrl;
_dio.options.connectTimeout = 5000;
_dio.options.receiveTimeout = 3000;
_dio.interceptors.add(LogInterceptor(responseBody: true)); //是否开启请求日志
}
}
测试是否每次都得到一样的实例
var s = identical(a, b);
print("选中s index = $s");