当前位置: 首页 > 知识库问答 >
问题:

我试图运行两个await和一个Navigator,推入一个异步函数,我在flutter中出现了这个错误

张永嘉
2023-03-14

错误:[error:flutter/lib/ui/ui_dart_state.cc(186)]未处理的异常:查找停用小部件的祖先是不安全的。E/Flutter(6342):此时,小部件的元素树的状态不再稳定。E/Flutter(6342):为了在小部件的dispose()方法中安全地引用其祖先,可以通过在小部件的didChangeDependencies()方法中调用dependOnInheritedWidgetOfExactType()来保存对祖先的引用。E/Flutter(6342):#0 element._DebugCheckStateIsActiveForExcestorLookup。(包:flutter/src/widgets/framework.dart:3864:9)e/flutter(6342):#1 element._debugcheckstateisactiveforcestorlookup(包:flutter/src/widgets/framework.dart:3878:6)e/flutter(6342):#2 element.findancestorstateoftype(包:flutter/src/widgets/framework.dart:3926:12)e/flutter(6342):#3navigator.of(包ART:2592:15)E/Flutter(6342):#5 NotificationDialog.CheckAvailabilityOfride(包:drivers_app/Notifications/NotificationDialog.Dart:240:20)E/Flutter(6342):

我的代码片段:

 void checkAvailabilityOfRide(context) async {
    
      var rideRequestId = await rideRequestRef.once();
           ...
      var dataSnapShot2 =  await newRequestsRef.child(rideRequestId.value).once();
           ...
     Navigator.push(context, MaterialPageRoute(builder: (context)=> 
      NewRideScreen(rideDetails: rideDetails)));
         ...
   }

更新代码:

 Widget build(BuildContext context) {
 return Dialog(
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
  backgroundColor: Colors.transparent,
  elevation: 1.0,
  child: Container(
    margin: EdgeInsets.all(5.0),
    width: double.infinity,
    decoration: BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(5.0),
    ),
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(height: 30.0),
        Image.asset("images/taxi.png", width: 120.0,),
        SizedBox(height: 18.0,),
        Text("New Ride Request", style: TextStyle(fontFamily: "Brand-Bold", fontSize: 18.0,),),
        SizedBox(height: 30.0),
        Padding(
          padding: EdgeInsets.all(18.0),
          child: Column(
            children: [

              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Image.asset("images/pickicon.png", height: 16.0, width: 16.0,),
                  SizedBox(width: 20.0,),
                  Expanded(
                    child: Container(child: Text(rideDetails.pickup_address, style: TextStyle(fontSize: 18.0),)),
                  ),
                ],
              ),
              SizedBox(height: 15.0),

              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Image.asset("images/desticon.png", height: 16.0, width: 16.0,),
                  SizedBox(width: 20.0,),
                  Expanded(
                    child: Container(child: Text(rideDetails.dropOff_address, style: TextStyle(fontSize: 18.0),)),
                  )
                ],
              ),
              SizedBox(height: 15.0),

            ],
          ),
        ),

        SizedBox(height: 20.0),
        Divider(height: 2.0,color: Colors.black, thickness: 2.0,),
        SizedBox(height: 8.0),

        Padding(
          padding: EdgeInsets.all(20.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [

              FlatButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(18.0),
                    side: BorderSide(color: Colors.red)),
                color: Colors.white,
                textColor: Colors.red,
                padding: EdgeInsets.all(8.0),
                onPressed: ()
                {
                  assetsAudioPlayer.stop();
                  Navigator.pop(context);
                },
                child: Text(
                  "Cancel".toUpperCase(),
                  style: TextStyle(
                    fontSize: 14.0,
                  ),
                ),
              ),

              SizedBox(width: 25.0),

              RaisedButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(18.0),
                    side: BorderSide(color: Colors.green)),
                onPressed: ()
                {
                  assetsAudioPlayer.stop();
                  checkAvailabilityOfRide0(context);
                  checkAvailabilityOfRide(context);
                },

                color: Colors.green,
                textColor: Colors.white,
                child: Text("Accept".toUpperCase(),
                    style: TextStyle(fontSize: 14)),
              ),

            ],
          ),
        ),

        SizedBox(height: 10.0),
      ],
    ),
  ),
);

}

Future<void> checkAvailabilityOfRide(context) async {
   var rideRequestId = await rideRequestRef.once();
   Navigator.push(context);
   String theRideId = "";
   var dataSnapShot2 =  await newRequestsRef.child( rideRequestId.value).once();
   /*... process...*/

   if(theRideId == rideDetails.ride_request_id && r2canShare) {
   rideRequestRef.set("accepted");
   AssistantMethods.disableHomeTabLiveLocationUpdates();
   Navigator.push(context, MaterialPageRoute(builder: (context) => 
   NewRideScreen(rideDetails: rideDetails)));
 }
   else if(theRideId == 'cancelled') {
   displayToastMessage("Ride has been Cancelled.", context);
   }
   else if(theRideId == 'cancelled') {
   displayToastMessage("Ride has been time out.", context);
   }
   else if(r2canShare == false) {
   assetsAudioPlayer.stop();
   displayToastMessage("Rider 2 cannot shared ride, out of range.", context);
   }
   else {
   displayToastMessage("Ride not exists.", context);
   }

}

更改代码

  void checkAvailabilityOfRide(context)async{
    var dataSnapShot2 =  await newRequestsRef.child( (await 
      rideRequestRef.once()).value).once();
    Navigator.pop(context);
    String theRideId = "";
    /*... process...*/

共有1个答案

程俊力
2023-03-14

@Nin Yu,过去几天,我也遇到了同样的错误,我试图用@Patrick answer修复它,但有时也不起作用。当我仔细调试它时,我发现我多次调用包含导航器的方法。因此,有时我得到了上下文,我安全地传递到下一个屏幕,有些它抛出了一个错误。我建议使用debug方法,并检查它是否出现。

 类似资料:
  • 例外情况: org.apache.tomcat.util.net.nioEndpoint$socketprocessor.dorun(NioEndpoint.java:1376)org.apache.tomcat.util.net.socketprocessorbase.run(Socketprocessorbase.java:49)java.base/java.util.concurrent.t

  • 我的IDE是Eclipse IDE 2020.03,使用由gradlew setDecompWorkspace和gradlew eclipse制作的Minecraft mods目录 我已经尝试将-vm目录更改为jdk 1.8.0和jre/bin/javaw.exe 错误显示:错误:无法创建Java虚拟机。错误:发生致命异常。程序将退出。 代码和eclipse。ini看起来像这样:

  • 我正在尝试使用flutter构建一个应用程序。当我尝试运行此推荐“flutter build appbundle”时,我收到以下错误: 失败:构建失败,但有例外。 > 其中:构建文件'F:\azabaza_app\myapp\android\app\build.gradle'行:28 错误:评估项目“app”时出现问题。 无法为FlutterExtension类型的扩展“flutter”获取未知属

  • 当我激活这个插件时,它会向我显示错误,我如何解决这个问题? 致命错误:未捕获错误:在C:\xampp\htdocs\www\real\wp content\plugins\realtyelite theme plugin\inc\vc函数中调用未定义的函数vc\u add\u shortcode\u param()。php:3堆栈跟踪:#0 C:\xampp\htdocs\www\real\wp

  • 问题是,我正在测试一个方法,但它给我一个404错误,我已经把我的承载令牌在我的请求中,这是合乎逻辑的,因为我需要把头“授权”。当我在《邮差》中提出这个要求时,它给了我很好的结果。我有什么不及格的? monedaServiceTest.java: 输出控制台:

  • 错误:任务“app:transformClassesWithDexForDebug”的执行失败 com.android.build.api.transform。TransformException:com.android.ide.common.process。ProcessException:org.gradle.process.internal。ExecException:处理C:\Progra