【问题标题】:Why async-await not working in this case?为什么 async-await 在这种情况下不起作用?
【发布时间】:2020-03-15 05:57:29
【问题描述】:

为什么async-await 在我的情况下不起作用?我在pageA中有一个按钮。点击后会弹出删除确认对话框。

一旦点击确认对话框中的Yes按钮,它将从服务器中删除数据并返回一个成功码,即101返回给PageA。

一旦pageA收到返回值,就会刷新ListView

PageA 按钮

onTap: () async {
  Navigator.pop(context);
  var result = await PopUpDialog().showDeleteDialog();
  if (result == 101) {
    setState(() {
      data.removeAt(index);   // remove the index in listView
    });
  } else {
    print('fjeodpedp');
  }
},

PopUpDialog

class PopUpDialog {
  var result;
  Future<int> showDeleteDialog() async {
         ....
    showDialog(
        context: context,
        builder: (BuildContext buildContext) {
          return AlertDialog(
              actions: <Widget>[
                FlatButton(
                  color: Colors.orange,
                  child: Text('YES', style: TextStyle(color: Colors.white)),
                  onPressed: () async {
                    Navigator.pop(buildContext);
                    result = await _bloc.delete();   // delete server data
                    if (result == 101) {
                      return result;
                    }
                  },
                ),
                FlatButton(
                  color: Colors.white,
                  child: Text('CANCEL'),
                  onPressed: () {
                    Navigator.of(buildContext, rootNavigator: true)
                        .pop('dialog');
                  },
                )
              ],
              ....
        });

    return 111; 
  }
}

但问题是,一旦弹出删除确认对话框,我会得到 fjeodpedp

【问题讨论】:

    标签: listview flutter dart async-await


    【解决方案1】:

    我通常将 Function 参数传递给我的 showDialog 函数,例如 onYesonCancel,然后从 onTap 调用它们对话框中的 em> 或 onPressed 事件。

    这是一个基于您的原始代码的功能示例:

    import 'package:flutter/material.dart';
    
    class TestPage extends StatefulWidget {
      @override
      createState() => _TestPageState();
    }
    
    class _TestPageState extends State<TestPage> {
      final int index = 5; // Sample index
      final data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Sample data
    
      @override
      Widget build(BuildContext context) {  
        return Container(
          child: RaisedButton(
            child: Text("Click me"),
            onPressed: () {
              showDeleteDialog(
                onYes: () { // This will be called when user taps 'YES'
                  setState(() {
                    data.removeAt(index); // remove the index in listView
                    print("removed");
                  });
                },
                onCancel: () { // This will be called when user taps 'CANCEL'
                  print('fjeodpedp');
                }
              );
            },
          ),
        );
      }
    
      void showDeleteDialog({Function onYes, Function onCancel}) async {
        showDialog(
          context: context,
          builder: (BuildContext buildContext) {
            return AlertDialog(
              actions: <Widget>[
                FlatButton(
                  color: Colors.orange,
                  child: Text('YES', style: TextStyle(color: Colors.white)),
                  onPressed: () async {
                    Navigator.pop(buildContext);
                    if (null != onYes) onYes(); // Ensure the reference exists before calling it
                  },
                ),
                FlatButton(
                  color: Colors.white,
                  child: Text('CANCEL'),
                  onPressed: () {
                    Navigator.pop(buildContext);
                    if (null != onCancel) onCancel(); // Ensure the reference exists before calling it
                  },
                )
              ],
            );
          }
        );
      }
    }
    

    希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

      我认为如果你这样做会更好。

      Future<int> showDeleteDialog() async {
       final response = showDialog(
          context: context,
          builder: (BuildContext buildContext) {
            return AlertDialog(
                actions: <Widget>[
                  FlatButton(
                    color: Colors.orange,
                    child: Text('YES', style: TextStyle(color: Colors.white)),
                    onPressed: () async {
                      Navigator.pop(buildContext,'yes');
                    },
                  ),
                  FlatButton(
                    color: Colors.white,
                    child: Text('CANCEL'),
                    onPressed: () {
                      Navigator.pop(buildContext,'cancel');
                    },
                  )
                ]
            );
          }
      );
      
        if(await response == 'yes'){
          return 101;
        }
        return 111;}
      

      【讨论】:

      • 我应该把result = await _bloc.delete();放在哪里?
      • 在if中输入yes按钮的功能
      【解决方案3】:

      您在显示对话框后立即返回 111。不确定您的意图是什么,但在我看来您不需要这一行,因为您正在处理页面 A 上的返回值。

      尝试删除这条线,看看它是怎么回事。

      【讨论】:

      • 我已将其删除,但没有任何区别。
      【解决方案4】:

      由于showDialog 是异步的,所以你在对话结束之前return 111

      您可以尝试使用setState((){}) 代替这个,这样它就不会太乱了

      【讨论】:

      • 我应该在setState写什么?
      • 有一个名为result的全局变量,然后在setState中设置它,然后你可以在你的小部件中处理不同的结果值。
      • 没有为类“PopUpDialog”定义方法“setState”
      猜你喜欢
      • 1970-01-01
      • 2018-07-15
      • 2019-12-04
      • 2017-11-26
      • 2010-12-29
      • 1970-01-01
      • 2021-08-05
      • 2011-11-14
      • 1970-01-01
      相关资源
      最近更新 更多