Urvashi kharecha
Urvashi kharecha

Reputation: 869

How to show multiple alert dialog in flutter one bye one?

I want to display alert dialog as per my rest API gives the data array.

I mean for example my rest API gives me data like :

"data": [
        {
            "id": "6",
            "user_id": "315",
            "message": "http://www.google.com",
            "status": "active",
            "date": "2020-04-09 17:18:00",
            "created": "2020-04-06 04:48:08"
        },
        {
            "id": "7",
            "user_id": "315",
            "message": "http://www.google.com",
            "status": "active",
            "date": "2020-04-09 17:18:00",
            "created": "2020-04-06 04:49:46"
        },
]

i want to display alert dialog as per my data length in flutter.

Upvotes: 1

Views: 5030

Answers (2)

CopsOnRoad
CopsOnRoad

Reputation: 268444

void _showDialog(int count) async {
  if (count <= 0) {
    return;
  } else {
    await showDialog(context: context, builder: (_) => AlertDialog());
    _showDialog(--count);
  }
}

Use it like:

_showDialog(3); // shows dialog 3 times

Upvotes: 0

wxker
wxker

Reputation: 1896

You can chain the dialogs with a recursive function.

void recursiveShowDialog(List arr, int index) async {
    if (index >= arr.length) {
        return;
    }
    await showDialog( *dialog code here with arr[index]*);
    recursiveShowDialog(arr, index + 1);
}

Then you can call this function where you want the dialogs to start

recursiveShowDialog(data, 0);

Where data is the list of items to be used in the dialog.

Upvotes: 4

Related Questions