【发布时间】:2020-06-30 12:58:21
【问题描述】:
Flutter 新手。 这是我主页的内容:
void main() => runApp(MaterialApp(
initialRoute: '/location',
routes: {
'/': (context)=> Loading(),
'/location': (context)=> Location(),
'/home': (context)=> Home(),
},
));
考虑在我的'/': (context)=> Loading(), 中有一个在initState 中执行的函数,在该函数结束时它重定向到'/home',但正如您在我的main.dart 中看到的那样,我将初始路由设置为@ 987654327@ 所以模拟器不应该打开加载文件以便重定向到主文件,但是它一直在发生。
当我清理并运行我的模拟器时,它会在位置文件上打开,然后以某种方式执行加载文件中的函数,这会将我带回主文件。
我不知道我是否有道理,如果你很难理解,请告诉我。
可选,这是我的loading.dart 函数:
class _LoadingState extends State<Loading> {
String time;
void fetchIt() async {
final WorldTimeClass defaultinstance = WorldTimeClass(flag: 'Algiers', url: 'Africa/Algiers', location: 'Algiers');
DateTime getTimeFormat = await defaultinstance.getTimeFormat();
await defaultinstance.getTimeHumanFormat(getTimeFormat);
// here the navigator to home.dart
await Navigator.pushReplacementNamed(context, '/home', arguments: {
'location': defaultinstance.location,
'time': defaultinstance.time,
'isDayTime': defaultinstance.isDayTime
});
}
@override
void initState() {
time = 'loading...';
super.initState();
fetchIt(); // here the function executed
}
...
请解释一下为什么会这样?
【问题讨论】:
-
你可以尝试在 PostFrameCallback 中包装 fetchit,stackoverflow.com/a/61886555/10238539
-
我解决了,查看下面的答案。