【问题标题】:Flutter Routes Emulators keeps rendering '/' instead of InitialRouteFlutter Routes Emulators 不断渲染“/”而不是 InitialRoute
【发布时间】: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
  }
...

请解释一下为什么会这样?

【问题讨论】:

标签: flutter dart navigator


【解决方案1】:

我通过在 routes: 参数中更改 - 解决了这个问题:

'/home''home'

'/location''location',以及

保持'/' 不变。

我不知道为什么这解决了问题,但是现在可以了。

【讨论】:

【解决方案2】:

'/' 表示的路由必须是您的初始路由,即您希望在应用启动时显示的屏幕。否则,如果您对任何其他屏幕使用“/”,该屏幕将在幕后运行。使用下面的代码作为示例。我想将第 2 页显示为主屏幕,这就是为什么我将初始路由设置为 '/' 和

'/':(BuildContext ctx)=>Page2()

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

// import 'package:path_provider/path_provider.dart';

void main() => runApp(Example());

class Example extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(

      title: "Example",
      initialRoute: '/',
      routes: {
         '/':(BuildContext ctx)=>Page2(),
        '/page1':(BuildContext ctx)=>Page1(),
        '/page3':(BuildContext ctx)=>Page3()

      },

    );
  }
}

class Page1 extends StatefulWidget{
  Page1State createState()=> Page1State();

}

class Page1State extends State<Page1> {

  @override
  void initState() {
    super.initState();
    timerFunction();

  }

  Timer timerFunction(){

    return new Timer(Duration(seconds: 3),handleTimeout);
}

handleTimeout(){
   Navigator.pushNamed(context, '/page3');
}

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Center(
        child: Text("Page1"),
      ),
    );
  }


}

class Page2 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body:Container(
        alignment: Alignment.center,
        child :Column(

        children:<Widget>[
          Padding(padding: EdgeInsets.only(top: 100)),
          Text("Page2"),
          RaisedButton(
            child: Text("go to page1"),
            onPressed: ()=>Navigator.pushNamed(context, '/page1'),
          )

        ]
      ),
      ),
    );
  }

}
class Page3 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Center(
        child: Text("Page3"),
      ),
    );
  }


}

【讨论】:

    【解决方案3】:

    首先,尝试通过重新运行flutter run重新启动应用程序

    【讨论】:

    • 我做过,也做过flutter clean
    • 尝试将初始路由设置为'/': (context)=&gt; Location(),
    • 和初始路由initialRoute: '/',
    • 这真的会适得其反,因为initialRoute 的存在是为了避免像这样的解决方法。
    • 为了更清楚地了解运行应用程序时究竟发生了什么
    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2016-04-08
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多