Navigator.pushNamed在Flutter中不起作用

时间:2019-11-10 06:05:56

标签: flutter

我正在开发Flutter应用程序。它包含一个Tab,并且在TabBarView里面是我的代码

body: TabBarView(
       children: <Widget>[
         notifications.Index(),
         Container(
           child: IconButton( onPressed: ()=>Navigator.pushNamed(context, '/notification'),icon:
              Icon(Icons.check_circle),),
         ),
       ],
     ),

在notification.index里面,这是我的代码


class Index extends StatefulWidget {
  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<Index> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
           child: IconButton( onPressed: ()=>Navigator.pushNamed(context, '/notification'),icon:
              Icon(Icons.check_circle),),
         ),
      ],
    );
  }
}


此导航在TabBarView中工作正常,并且在notification.index中生成以下错误

════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
Could not find a generator for route RouteSettings("/notification", null) in the _WidgetsAppState.

如果更改notificationRoute的notification.index文件导航,如下所示

Navigator.push(context, MaterialPageRoute(builder: (BuildContext context)=>(Index()),),);},

工作正常。 请问我是否可以在不使用Navigator.push()的情况下克服此错误?如果有的话?

1 个答案:

答案 0 :(得分:0)

如果要使用Navigator.pushNamed在主dart MaterialApp小部件中添加路线,则类似于下面的代码,否则Flutter无法知道其路线

    return MaterialApp(

  routes: {
        '/': (ctx) => MyHomePage(),
       "/notification": (ctx) => NotificationScreen(),
      },
)