我正在努力学习dart 和Flutter Flutter 的语言.我看到了以下代码段(摘自此处:https://github.com/ppicas/flutter-android-background/blob/master/lib/counter_service.dart):
import 'package:flutter/foundation.dart';
import 'counter.dart';
class CounterService {
factory CounterService.instance() => _instance;
CounterService._internal(); <<====================== Why is this line here ? What does it do?
static final _instance = CounterService._internal();
final _counter = Counter();
ValueListenable<int> get count => _counter.count;
void startCounting() {
Stream.periodic(Duration(seconds: 1)).listen((_) {
_counter.increment();
print('Counter incremented: ${_counter.count.value}');
});
}
}
我弄不明白为什么那里需要这行代码.我知道我们通过实现私有构造函数来强制返回这个类的单个实例,而唯一的实例是在下面一行中创建的.那么,为什么我们真的要在那里有这条线呢?
如果dart 专家能给我解释一下,我将不胜感激.谢谢