【问题标题】:Reload current screen when an exception occurs发生异常时重新加载当前屏幕
【发布时间】:2018-09-11 10:22:55
【问题描述】:

我有一个屏幕,用户可以在上面输入一些数字,然后他会确认。确认进行 API 调用。此调用可能会失败。当它失败时,我会显示AlertDialogshowDialog。 我想要的是,当用户关闭对话框时,屏幕会刷新/重新加载。输入数据应该被删除(还有一些其他的效果也应该被重置)。

我不确定实现这一目标的最佳方法是什么。

  void _handleError(e) {
    showDialog(
      context: context,
      builder: (BuildContext builder) {
        return AlertDialog(
          title: Text(e.toString()),
          content: Text('Some content'),
        );
      },
    );
    // I'm guessing I should do something here?
  }

  // This is the handler for the confirm buttons `onPressed` field. 
  void _pay(context) {
    double amount = double.parse(textFieldController.text);

    apiClient
        .createInvoice(amount)
        .then((Map<String, dynamic> invoice) {
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) {
          return Pay(invoice);
        }),
      );
    }).catchError(handleError);
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    showDialog() 函数在被解除时返回 Future。因此,您可以将 then() 调用链接到 showDialog() 并在那里进行清理。

    void _handleError(e) {
      showDialog(
        context: context,
        builder: (BuildContext builder) {
          return AlertDialog(
            title: Text(e.toString()),
            content: Text('Some content'),
          );
        },
      ).then((_){
        //do your clean up
        _inputTextController.text = '';
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多