Flutter中的Singleton析构函数

问题描述

一旦创建了一个单例,该如何破坏其中的代码我有一个警告显示在消耗StreamController的单例中,它需要关闭。但是没有可以调用close()函数方法

这是我目前拥有的单例代码StreamController正在创建我要修复的警告。

class MyService {
  static final MyService _instance = MyService._privateConstructor();

  StreamController<List> _streamController = StreamController<List>.broadcast();

  factory MyService() => _instance;

  MyService._privateConstructor() {}
}

解决方法

也许我错过了一些东西,但是如果您有单身人士,是什么使您无法在单身人士上实现https://xx.xx.xx.xx:8443/pentaho/方法?

,

尝试使用此功能:

// Disposable interface
abstract class Disposable {
  void dispose();
}

// Target class
class MyService implements Disposable {
  static final MyService _instance = MyService._privateConstructor();
  StreamController<List> _streamController = StreamController<List>.broadcast();
  factory MyService() => _instance;
  MyService._privateConstructor() {}

  @override
  void dispose() {
    _streamController.close();
  }
}

// Example of call
MyService.dispose();