本文主要是介绍Flutter异常 NoSuchMethodError The getter focusScopeNode was called on null,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在启动新页面是出现异常:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The
getter 'focusScopeNode' was called on null.
E/flutter (26425): Receiver: null
E/flutter (26425): Tried calling: focusScopeNode
E/flutter (26425): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (26425): #1 Route.didPush.<anonymous closure> (package:flutter/src/widgets/navigator.dart:139:17)
错误代码:
void _onPass(){Navigator.push(context, MaterialPageRoute(builder: (context){return DetailPage();}));Navigator.pop(context);}
出现异常的原因是在刚push界面之后,不能立即调用Navigator.pop(context),
Navigator.pop方法看源码声明:
/// Pop the top-most route off the navigator.////// {@macro flutter.widgets.navigator.pop}////// {@tool sample}////// Typical usage for closing a route is as follows:////// ```dart/// void _handleClose() {/// navigator.pop();/// }/// ```/// {@end-tool}/// {@tool sample}////// A dialog box might be closed with a result:////// ```dart/// void _handleAccept() {/// navigator.pop(true); // dialog returns true/// }/// ```/// {@end-tool}@optionalTypeArgsbool pop<T extends Object>([ T result ]) {assert(!_debugLocked);assert(() {_debugLocked = true;return true;}());
可以将页面路由当做一个栈,在刚push进去一个页面时,
立即调用Navigator.pop(context)会将栈顶的页面删除,此时push的页面就不能正常启动,会报上述异常
看源码,官方给的推荐用法:
/// The state from the closest instance of this class that encloses the given context.////// Typical usage is as follows:////// ```dart/// Navigator.of(context)/// ..pop()/// ..pop()/// ..pushNamed('/settings');/// ```////// If `rootNavigator` is set to true, the state from the furthest instance of/// this class is given instead. Useful for pushing contents above all subsequent/// instances of [Navigator].
修改之后代码:
void _onPass(){Navigator.of(context)..pop()..push(MaterialPageRoute(builder: (context){return DetailPage();}));}
或者
void _onPass(){Navigator.pop(context);Navigator.push(context, MaterialPageRoute(builder: (context){return DetailPage();}));}
这篇关于Flutter异常 NoSuchMethodError The getter focusScopeNode was called on null的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!