NoSuchMethodError:方法“ ancestorStateOfType”在null上调用 [英] NoSuchMethodError: The method 'ancestorStateOfType' was called on null
本文介绍了NoSuchMethodError:方法“ ancestorStateOfType”在null上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在像这样的流媒体列表进行屏幕更改,当它发出时,我会更改屏幕
i am doing screen change like this iam listing for stream and when it emit i change the screen
@override
void initState() {
super.initState();
appBloc.error.listen((data) {
_scaffoldKey.currentState.showSnackBar(new SnackBar(content: new
Text(data)));
});
appBloc.success.listen((_) => goToDashBoardScreen(context));
}
和doToDashBoardScreen看起来像这样
and doToDashBoardScreen look like this
Navigator.pushReplacement(context, new SlideRightRoute(widget:
DashBoardScreen()));
但是我遇到这样的错误,但是我改变了页面。
but i am getting error like this but i change the page.
22:05:02.446 3 info flutter.tools E/flutter (13216): NoSuchMethodError:
The method 'ancestorStateOfType' was called on null.
22:05:02.446 4 info flutter.tools E/flutter (13216): Receiver: null
22:05:02.446 5 info flutter.tools E/flutter (13216): Tried calling:
ancestorStateOfType(Instance of 'TypeMatcher<NavigatorState>')
22:05:02.446 6 info flutter.tools E/flutter (13216): #0
Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:48:5)
22:05:02.446 7 info flutter.tools E/flutter (13216): #1
Navigator.of (package:flutter/src/widgets/navigator.dart:1270:19)
22:05:02.446 8 info flutter.tools E/flutter (13216): #2
Navigator.pushReplacement
(package:flutter/src/widgets/navigator.dart:952:22)
推荐答案
您的窗口小部件最有可能从树上移开。因此,它不再有上下文
。
Your widget has most likely been removed from the tree. Therefore it doesn't have a context
anymore.
问题是,您忘记了退订流
。因此,即使将其从树中删除后,您的小部件仍会尝试更新。
The problem being, you forgot to unsubscribe to your Stream
. Therefore even after being removed from the tree, your widget still attempts to update.
一种解决方案是取消对处置
的认购:
A solution would be to unsubscribe on dispose
call:
class Foo extends StatefulWidget {
@override
_FooState createState() => _FooState();
}
class _FooState extends State<Foo> {
StreamSubscription streamSubscription;
@override
void initState() {
super.initState();
streamSubscription = Bloc.of(context).myStream.listen((value) {
print(value);
});
}
@override
void dispose() {
streamSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
这篇关于NoSuchMethodError:方法“ ancestorStateOfType”在null上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文