4

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');
           }
         },

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;
    },

Bloc class

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

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.

What is the correct way?

Thanks for your valuable time.

0

1 Answer 1

3

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(); 

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.

Update

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;
  }

Your success function would be like this

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

onFailure(){
//Add your failiure logic here.
}
Sign up to request clarification or add additional context in comments.

9 Comments

@Tony it won't work this way because you're directly returning 0 in the return so you'll return 0 every time.
You can do one thing pass your current function as argument in the showDialog method. And call that function on the confirmation. If you're confused then i can give you a short demo about it.
Updated the answer. Please take a look
I still don't understand. What is onSuccess and onFailure?
Pass that index into showDialog method and then re-pass that index into success function. This way you can manage that issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.