The context used to push or pop routes from the Navigator must be that of a widget that is a descend

使用flutter的Navigator.push跳转页面

The context used to push or pop routes from the Navigator must be that of a widget that is a descend

我们先看这段代码:

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // 确定初始化
  SystemChrome.setPreferredOrientations(// 使设备横屏显示
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  SystemChrome.setEnabledSystemUIOverlays([]); // 全屏显示
  runApp(RouterTestRoute());
}

class NewRoute extends StatelessWidget{
    NewRoute({
      Key? key,
      required this.text,
     }):super(key:key);
    final String text;
    @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp (
        home:Scaffold(
      appBar:AppBar(
          title:Text('New Route'),
                  ),
      body:Center(
          child:Column(
            children: [
              Text(text,textDirection:TextDirection.ltr),
              RaisedButton(
                onPressed: ()=>Navigator.pop(context,"back data"),
                child: Text('back'),
              )
            ],
          )

      ),
    )
    );
  }
}

class RouterTestRoute extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
       home:
          Center(
        child: RaisedButton(
          child: Text('click'),
        onPressed: () async{
          var result = await Navigator.push(context,MaterialPageRoute( builder: (BuildContext  context) {

                 return new NewRoute(text:'传入1');
            }
            ),

          );
          print("路由返回值: "+result);
        },
      ),
    )
    );
  }
}

我们在使用flutter页面跳转api的时候会遇到The context used to push or pop routes from the Navigator must be that of a widget that is a descend这样的错误描述,意思是使用的Context必须是Navigator Widget的子类,
使用Navigator.push(context,MaterialPageRoute( builder: (BuildContext context)的时候我们要是有MaterialApp的上下文,但是当前context是StatelessWidget的实现类的上下文,所有我们需要拿到MaterialApp上context,
这个是修改后的代码:

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // 确定初始化
  SystemChrome.setPreferredOrientations(// 使设备横屏显示
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  SystemChrome.setEnabledSystemUIOverlays([]); // 全屏显示
  //下面是修改后的代码
  runApp( MaterialApp(home:RouterTestRoute()));
}

class NewRoute extends StatelessWidget{
    NewRoute({
      Key? key,
      required this.text,
     }):super(key:key);
    final String text;
    @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp (
        home:Scaffold(
      appBar:AppBar(
          title:Text('New Route'),
                  ),
      body:Center(
          child:Column(
            children: [
              Text(text,textDirection:TextDirection.ltr),
              RaisedButton(
                onPressed: ()=>Navigator.pop(context,"back data"),
                child: Text('back'),
              )
            ],
          )

      ),
    )
    );
  }
}

class RouterTestRoute extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
     //下面是修改后的代码 返回一个Scaffold
    return new Scaffold(
       body:Center(
        child: RaisedButton(
          child: Text('click'),
        onPressed: () async{
          var result = await Navigator.push(context,MaterialPageRoute( builder: (BuildContext  context) {

                 return new NewRoute(text:'传入1');
            }
            ),

          );
          print("路由返回值: "+result);
        },
      ),
    )
    );
  }
}

具体context可以参考这个:深入理解BuildContext

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值