【问题标题】:Navigator.push throw unhandled exception inside onTap witth asyncNavigator.push 在带有异步的 onTap 中抛出未处理的异常
【发布时间】:2021-07-14 21:37:15
【问题描述】:

我有一个简短的问题,为什么我不能在 onTap 中使用 Navigator.push,而 async 在那里。删除 asyncawait 函数可以正常工作。 Navigator.push 可以正常工作。但是在 onTap 内部的未来函数调用中,我无法导航到新类。

  onTap: () async {
       String _courierOrderID = await model.placeCourierOrder(widget.orderInfoMap);
    // placeCourierOrder is just a Future<String> type function and it works fine. Order get placed successfully.
  
print("------- Courier Order Placed  " + _courierOrderID.toString());
print("context: " + _courierOrderID.toString());
  
  Navigator.push(context,
     MaterialPageRoute(
        builder: (context) => CourierOrderSummaryView(singleCourierDataID: _courierOrderID),
                 ),
               );
             },

输出 从下面的代码输出中,您可以看到我的订单已成功下单,但在 Navigator.push 行它给出了未处理的异常

------- Courier Order Placed  501-1c614
I/flutter (16526): context: LayoutBuilder(renderObject: _RenderLayoutBuilder#f6291 relayoutBoundary=up1 NEEDS-LAYOUT DETACHED)

E/flutter (16526): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (16526): At this point the state of the widget's element tree is no longer stable.
E/flutter (16526): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
E/flutter (16526): #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure>
package:flutter/…/widgets/framework.dart:3864
E/flutter (16526): #1      Element._debugCheckStateIsActiveForAncestorLookup
package:flutter/…/widgets/framework.dart:3878
E/flutter (16526): #2      Element.findAncestorStateOfType
package:flutter/…/widgets/framework.dart:3926
E/flutter (16526): #3      Navigator.of
package:flutter/…/widgets/navigator.dart:2706
E/flutter (16526): #4      Navigator.push
package:flutter/…/widgets/navigator.dart:2116
E/flutter (16526): #5      _CourDelivReviewState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:storeifie/…/courierViews/courDelivReview.dart:560
E/flutter (16526): <asynchronous suspension>
E

【问题讨论】:

    标签: flutter dart asynchronous async-await navigation


    【解决方案1】:

    也许这对你有用

      onTap: () async {
           String _courierOrderID = await model.placeCourierOrder(widget.orderInfoMap);
      
      
        await Navigator.push(context,
         MaterialPageRoute(
            builder: (context) => CourierOrderSummaryView(singleCourierDataID: _courierOrderID),
                 ),
             );
          },
    

    【讨论】:

    • 这对人没有帮助。同样的输出,现在不知道怎么办了。
    • @FaizanKamal 你能分享你的完整代码sn-p吗?
    【解决方案2】:
    onTap: () async {
           String _courierOrderID = await model.placeCourierOrder(widget.orderInfoMap);
        // placeCourierOrder is just a Future<String> type function and it works fine. Order get placed successfully.
      
    print("------- Courier Order Placed  " + _courierOrderID.toString());
    print("context: " + _courierOrderID.toString());
      
      if(!mounted){
        print('you are not in this context anymore");
        // you changed the page and now you can't use this context.
      }
      Navigator.push(context,
         MaterialPageRoute(
            builder: (context) => CourierOrderSummaryView(singleCourierDataID: _courierOrderID),
        ),
      );
    },
    

    【讨论】:

    • mounted 的结果是true。这意味着我仍然可以使用上下文对吗?我现在不知道该怎么办
    猜你喜欢
    • 2017-06-08
    • 2011-10-10
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2011-12-12
    相关资源
    最近更新 更多