未处理的异常:NoSuchMethodError:方法“findAncestorStateOfType”在 null 上被调用
Posted
技术标签:
【中文标题】未处理的异常:NoSuchMethodError:方法“findAncestorStateOfType”在 null 上被调用【英文标题】:Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null 【发布时间】:2020-09-28 13:33:17 【问题描述】:由于以下错误,我的应用卡在 HandleMainScreen。它第一次正确导航,但在控制台中出现错误,但是当我热重启时,导航不起作用,应用程序卡在 HandleMainScreen。
E/flutter ( 8785): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null.
E/flutter ( 8785): Receiver: null
E/flutter ( 8785): Tried calling: findAncestorStateOfType<NavigatorState>()
E/flutter ( 8785): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter ( 8785): #1 Navigator.of
package:flutter/…/widgets/navigator.dart:2115
E/flutter ( 8785): #2 _HandleMainScreenState.checkAuthentication.<anonymous closure>
package:monktree/main.dart:58
E/flutter ( 8785): #3 _rootRunUnary (dart:async/zone.dart:1192:38)
E/flutter ( 8785): #4 _CustomZone.runUnary (dart:async/zone.dart:1085:19)
E/flutter ( 8785): #5 _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)
E/flutter ( 8785): #6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter ( 8785): #7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:266:7)
以下代码是我正在使用的。如果您有任何更好的方式来处理用户,请在这个问题上回答它。
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
builder: (context, child)
return ScrollConfiguration(
behavior: MyBehavior(),
child: child,
);
,
home: HandleMainScreen(),
debugShowCheckedModeBanner: false,
);
class MyBehavior extends ScrollBehavior
@override
Widget buildViewportChrome(
BuildContext context, Widget child, AxisDirection axisDirection)
return child;
class HandleMainScreen extends StatefulWidget
@override
_HandleMainScreenState createState() => _HandleMainScreenState();
class _HandleMainScreenState extends State<HandleMainScreen>
final FirebaseAuth _auth = FirebaseAuth.instance;
bool isLoggedIn;
FirebaseUser _user;
String error;
@override
void initState()
super.initState();
checkAuthentication();
checkAuthentication() async
_auth.onAuthStateChanged.listen((user)
_user = user;
if (user == null)
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => LoginPage()));
else if (user != null)
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => HomePage(user)));
).onError((e)
error = e.toString();
print("Error in HandleMainScreen\n" + e.toString());
);
@override
Widget build(BuildContext context)
Constants.allocateSize(size: MediaQuery.of(context).size);
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
【问题讨论】:
【参考方案1】:改变这个:
checkAuthentication() async
_auth.onAuthStateChanged.listen((user)
_user = user;
if (user == null)
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => LoginPage()));
else if (user != null)
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => HomePage(user)));
).onError((e)
error = e.toString();
print("Error in HandleMainScreen\n" + e.toString());
);
进入这个:
checkAuthentication() async
_auth.onAuthStateChanged.listen((user)
_user = user;
if (user == null)
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => LoginPage()),
);
else if (user != null)
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => HomePage(user)),
);
).onError((e)
error = e.toString();
print("Error in HandleMainScreen\n" + e.toString());
);
【讨论】:
谢谢它现在可以工作了,但是当我注销或登录时出现同样的错误。我在他们的导航中做了同样的改变,但错误仍然存在。Error link 关于注销和登录时的错误,请提出不同的问题并将链接发送给我,以便我检查,但添加有关这些页面的特定代码 ***.com/questions/62282858/…以上是关于未处理的异常:NoSuchMethodError:方法“findAncestorStateOfType”在 null 上被调用的主要内容,如果未能解决你的问题,请参考以下文章
获取“未处理的异常:NoSuchMethodError:方法'map'在null上被调用”
未处理的异常:NoSuchMethodError:方法“findAncestorStateOfType”在 null 上被调用
Flutter,无法提取 api 数据:(未处理的异常:NoSuchMethodError:方法 'map' 在 null 上被调用。)
未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]”(json 解析)
未处理的异常:NoSuchMethodError:在 null 上调用了方法“toRawHandle”
未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]”。尝试调用:[](“is_ordered”)。检查空字段 Firebase [重复]