flutter setState not updating showCupertinoDialog

Viewed 1152

setState is not working in showCupertinoDialog. When i get async data i want to update the progress bar which is inside dialog. _progress value is updated and setstate is called successfully but dialog is not updating. I am using statefulBuilder.

 showCupertinoDialog(
      context: context,
      builder: (BuildContext context) => CupertinoAlertDialog(
        title: Text("Preparing..."),
        content: StatefulBuilder(builder: (context, StateSetter setState) {
          print('ssetstatte');
          return UploadIndicator(_progress);
        }),
        actions: [
          CupertinoDialogAction(
            onPressed: () {
              Navigator.of(context).pop();
            },
            isDefaultAction: true,
            child: Text("Cancel"),
          ),
        ],
      ),
    ).whenComplete(() {});



_uploadTask.snapshotEvents.listen((event) {
     
      _progress =
          event.bytesTransferred.toDouble() / event.totalBytes.toDouble();
/*THIS IS NOT UPDATING DIALOG*/
      setState(() {
        _progress = double.parse((_progress * 100).toStringAsFixed(2));
       
      });
});
3 Answers

when your async data is updating show that data in stateless widget statefull widget did not update the whole widget ,use stateless widget to show the data it update the state immediately,statefull widget set that data and stateless renders that data

Use StatefulBuilder to use setState inside Dialog and update Widgets only inside of it.

 showDialog(
  context: context,
  builder: (context) {
    String contentText = "Content of Dialog";
    return StatefulBuilder(
      builder: (context, setState) {
        return AlertDialog(
          title: Text("Title of Dialog"),
          content: Text(contentText),
          actions: <Widget>[
            FlatButton(
              onPressed: () => Navigator.pop(context),
              child: Text("Cancel"),
            ),
            FlatButton(
              onPressed: () {
                setState(() {
                  contentText = "Changed Content of Dialog";
                });
              },
              child: Text("Change"),
            ),
          ],
        );
      },
    );
  },
);

when you update your state, that time you have to use stfullWidget. Eensure first, is it stfullWidget or not? if not then you should use stfullWidget insted of stlessWidget.

Related