我有一个更改通知程序,可以打开一个Firestore快照,并且我想关闭该应用程序以关闭快照。我尝试在dispose方法中执行关闭操作,但是在关闭应用程序时永远不会调用它。
这是主要内容:
void main() {
runApp(MultiProvider(
providers: [
ChangeNotifierProvider<Auth>(create: (_) => Auth.instance()),
ChangeNotifierProxyProvider<Auth, Api>(
create: (_) => Api.instance(null),
update: (_, auth, api) => Api.instance(auth.user),
)
],
child: MyApp()
));
}
这是ChangeNotifier:
class Api extends ChangeNotifier {
StreamSubscription<QuerySnapshot> streamTests;
List<Test> tests;
FirebaseUser _user;
set user(FirebaseUser user) {
_user = user;
disposeStreams();
initStreams();
}
Api.instance(FirebaseUser user) {
_user = user;
tests = List<Test>();
debugPrint("Api.instance");
if (_user != null) {
debugPrint("user != null");
initStreams();
}
else {
debugPrint("user == null");
disposeStreams();
}
}
void initStreams() {
debugPrint("initStreams");
streamTests = Firestore.instance
.collection("tests")
.where("uid", isEqualTo: _user.uid)
.snapshots()
.listen((querySnapshot) {
debugPrint("querySnapshot tests " + querySnapshot.documents.length.toString());
tests = querySnapshot.documents
.map((d) => Test.fromMap(d.data, d.documentID))
.toList();
notifyListeners();
});
}
void disposeStreams() {
debugPrint("Dispose is not being called");
if (streamTests != null) {
streamTests.cancel();
}
}
@override
void dispose() {
disposeStreams();
super.dispose();
}
}
我在做什么错?这种方法不对吗?