flutter navigator pop dialog - error - Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null

flutter navigator pop dialog - error - Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null

我在 flutter 中得到了这段弹出 Dialog() 代码 https://gist.github.com/axilaris/2b186c7a4073671128e8cacc09dfc384,如果您查看底部某处的代码

class PurchaseDialog extends StatefulWidget with NavigationStates {
...
class _PurchaseDialogState extends State<PurchaseDialog> {
...
  @override
  Widget build(BuildContext context) {
    return Dialog(
...
 showSecondaryButton(BuildContext context) {
...
Navigator.of(context).pop(); <--- here is the problem

每当它调用

Navigator.of(context).pop();

会导致如下错误

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null.

这看起来像是这个 的副本,但我已经尝试过它不起作用的解决方案。 (context只能设置一次,检查mounted变量无效)

如何解决这个问题?到目前为止,它关闭了对话框(使用 pop() 所以它的正确行为),但我不希望出现此错误。

更新信息: 上面的PurchaseDialog()是这样调用的:

    showDialog(
      context: context,
      builder: (BuildContext context) => PurchaseDialog(),
    ).then((value) {
      setState(() {
      });
    });

这里是痕迹打印:https://gist.github.com/axilaris/6d8db8824b0b89e33fee7ddfdd238e34

阅读跟踪打印后,我们可以确定问题出在对话框关闭后未处理的 FlutterInappPurchase.purchaseUpdated.listen(...)FlutterInappPurchase.purchaseError.listen(...)。他们使用上下文弹出并打开一个新对话框(在 purchaseError 的情况下),此时该对话框可能为空。在处理小部件后取消流可以解决问题。很高兴知道这解决了问题

@override
void dispose(){
  super.dispose();
  _purchaseUpdatedSubscription.cancel();
  _purchaseErrorSubscription.cancel();
}