stack_wallet/lib/wallets/isar/providers/util/watcher.dart

32 lines
639 B
Dart
Raw Normal View History

2023-11-01 19:00:36 +00:00
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:isar/isar.dart';
import '../../isar_id_interface.dart';
2023-11-01 19:00:36 +00:00
class Watcher<T extends IsarId> extends ChangeNotifier {
late final StreamSubscription<T?> _streamSubscription;
T _value;
T get value => _value;
Watcher(
this._value, {
required IsarCollection<T> collection,
}) {
_streamSubscription = collection.watchObject(_value.id).listen((event) {
if (event != null) {
_value = event;
notifyListeners();
}
});
}
@override
void dispose() {
_streamSubscription.cancel();
super.dispose();
}
}