gpt4 book ai didi

flutter - Flutter错误:未处理的异常:NoSuchMethodError:在空调用了 'findAncestorStateOfType'方法

转载 作者:行者123 更新时间:2023-12-03 03:32:53 26 4
gpt4 key购买 nike

每次登录时,都会出现以下错误。
我发现Navigator.pushReplacement出现了一些问题,该问题在控制台中产生了错误,但仍然允许我进行导航。
不知道如何解决此错误。
以下代码是我正在使用的代码。请让我知道是否有任何错误可以解决。

class WelcomeScreen extends StatefulWidget {
@override
_WelcomeScreenState createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen>
with SingleTickerProviderStateMixin {
startTime() async {
var _duration = Duration(seconds: 5);
return Timer(_duration, navigationPage);
}

void navigationPage() {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => FirstTimeUserOnly(),
),
);
}

@override
void initState() {
super.initState();
startTime();
}


final int delayedAmount = 100;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Welcome'),
Logo(),
],
),
),
);
}
}
这是我得到的错误
E/flutter (16323): Tried calling: findAncestorStateOfType<NavigatorState>()
E/flutter (16323): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (16323): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:2115:19)
E/flutter (16323): #2 Navigator.pushReplacement (package:flutter/src/widgets/navigator.dart:1781:22)
E/flutter (16323): #3 _WelcomeScreenState.navigationPage (package:/welcome_screen.dart:23:15)
E/flutter (16323): #4 _rootRun (dart:async/zone.dart:1180:38)
E/flutter (16323): #5 _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter (16323): #6 _CustomZone.runGuarded (dart:async/zone.dart:979:7)
E/flutter (16323): #7 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
E/flutter (16323): #8 _rootRun (dart:async/zone.dart:1184:13)
E/flutter (16323): #9 _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter (16323): #10 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1003:23)
E/flutter (16323): #11 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
E/flutter (16323): #12 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter (16323): #13 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter (16323): #14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (16323):

最佳答案

试试这个,定时器给你错误

class WelcomeScreen extends StatefulWidget {
@override
_WelcomeScreenState createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen>
with SingleTickerProviderStateMixin {

Timer _timer;
void startTime() async{
_timer = Timer(new Duration(seconds: 5), navigationPage);
}

void navigationPage() {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => FirstTimeUserOnly(),
),
);
}

@override
void initState() {
super.initState();
startTime();
}

@override
void dispose() {
super.dispose();
_timer.cancel();
}

final int delayedAmount = 100;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Welcome'),
Logo(),
],
),
),
);
}
}

关于flutter - Flutter错误:未处理的异常:NoSuchMethodError:在空调用了 'findAncestorStateOfType'方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62887754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com