在Flutter中刷新ListView [英] Refresh ListView in Flutter

查看:1155
本文介绍了在Flutter中刷新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何刷新 ListView
在页面A中说,我有一个 ListView ,并且行项中有一个菜单图标。
当我单击菜单图标时,它将显示一个带有删除图标的底部对话框。
单击删除图标时,将弹出一个删除确认对话框。
单击确认对话框中的是按钮后,它将删除该项目。收到成功状态后,将刷新 ListView
这是底部工作表删除图标的代码

How can I refresh a ListView ? Let say in page A, I have a ListView, and there is a menu icon in the row item. When I click the menu icon, it will show a bottom sheet dialog which has a delete icon. When delete icon is clicked, it will pop up a delete confirmation dialog. Once the 'Yes' button in confirmation dialog is clicked, it will delete the item. Once it received the "Success" status, it will refresh the ListView. This is the code for bottom sheet delete icon

 onTap: () {
          Navigator.pop(context);
          var result =  PopUpDialog().showDeleteDialog();   // pop up confirmation dialog
          if (result == 'Success') {
                print('success');
                setState(() {
                  data.removeAt(index);
               });
           } else {
               print('fjeodpedp');
           }
         },

这是<$ c $的代码c>是在确认对话框中。

And this is the code for Yes button in confirmation dialog.

PopUpDialog-showDeleteDialog

  onPressed: () async {
      Navigator.pop(buildContext);   // dismiss confirmation dialog
          var result = await _bloc.delete();
          return result;
    },

集团阶级

Future delete() async {
    Response delete = await _repo.delete();  // delete data in server
    var deleteResponse = Response.fromJson(delete.body);
    return deleteResponse.status;   // return Success
  }

我想要 setState 仅在 deleteResponse.status 等于成功时调用,但是一旦弹出确认对话框,它将继续打印 fjeodpedp 。我添加了async-await,但仍然无法正常工作。

I want the setState get called only if deleteResponse.status is equal to success, but it keep printing fjeodpedp once the confirmation dialog is pop up. I have added async-await, but still not working.

正确的方法是什么?

感谢您的宝贵时间。

推荐答案

我认为您不是在等待对话框确认。您应该使用await方法来等待该对话框的结果。我不知道您在

I think you're not waiting for your dialog confirmation. You should use await method to wait for the result of that dialog. I don't know what you wrote in

PopUpDialog().showDeleteDialog();

方法,但是执行后续操作应该是异步的。

method but it should be async to perform the after operations.

现在您可以编写此语句来解​​决错误。

For now you can write this statement to resolve the error.

var result =  await PopUpDialog().showDeleteDialog(); 

或者如果showDeleteDialog()是异步的,也可以执行此操作->

Or you can also do this if you showDeleteDialog() is async ->

PopUpDialog().showDeleteDialog().then((result){
if (result == 'Success') {
                print('success');
                setState(() {
                  data.removeAt(index);
               });
           } else {
               print('fjeodpedp');
           }

});

尝试一下,让我知道它是否有效。

Try this and let me know whether it works or not.

更新

int showDeleteDialog({Function onSuccess, Function onFailure}) {
    _bloc = Provider.of<Bloc>(context);
    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);
                    var result = await _bloc.deleteWorkOrder();
                    if(onSuccess!=null) onSuccess(result); <------ Here you can pass your result directly to the function.
                    return result;
                  },
                ),
                FlatButton(
                  color: Colors.white,
                  child: Text('CANCEL'),
                  onPressed: () {
                    if(onFailure!=null) onFailure();
                    Navigator.of(buildContext, rootNavigator: true)
                        .pop('dialog');
                  },
                )
              ],
              title: Text(Localization.of(buildContext).deleteDialogTitle),
              content: Text(Localization.of(buildContext).deleteDialogContent));
        });

    return 0;
  }

您的成功功能将是这样

onSuccess(result){
           if (result == 'Success') {
                print('success');
                setState(() {
                  data.removeAt(index);
               });
           } else {
               print('fjeodpedp');
           }
}

onFailure(){
//Add your failiure logic here.
}

这篇关于在Flutter中刷新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆