In my Flutter application I need to allow admin users to download a CSV file with all the data entered into Firebase.
To do this, admins must press a button. If they do it from the web, the file is downloaded via the browser, while if they are on another device, a dialog opens asking if they want to download or share the file.
Creating the file takes some time, so I would like to show a CircularProgressIndicator
while waiting for the AlertDialog
to open.
I don’t know how to do this though, because I typically show the CircularProgressIndicator
when calling a Future
function via FutureBuilder
, but showDialog
doesn’t return a Widget
.
So how can I show a CircularProgressIndicator
before opening AlertDialog
?
Here is my code, thanks in advance!
FilledButton.icon(
onPressed: () async {
// This function creates a CSV file by retrieving data from Firebase,
// and takes some time to run
final String fileData = await getCsv();
if (kIsWeb) {
// running on the web
downloadFileOnWeb(fileData);
} else {
// NOT running on the web
if (!context.mounted) return;
return showDialog<void>(
context: context,
builder: (context) => AlertDialog(
content: const Text("Get report"),
actions: [
IconButton(
icon: const Icon(Icons.download),
onPressed: () => downloadFile(fileData),
),
IconButton(
icon: const Icon(Icons.share),
onPressed: () => shareFile(fileData),
),
],
),
);
}
},
icon: const Icon(Icons.file_open),
label: const Text("Report"),
),
2
Answers
Why don’t you simply use a
bool
to keep track of the loading state?For example:
In your state class, create:
and use it in your function:
and in your build, show different widgets based on the state;:
complete runnable snippet:
See also
To show a CircularProgressIndicator while waiting for your CSV file to load, you can use showDialog to display the loading indicator at the start of your function. Once the file is loaded, you can close the loading dialog and then open the AlertDialog for sharing and downloading the file. Here’s how you can achieve this:
Step-by-Step Implementation
Here’s a complete example of how to implement this:
For Your Reference
By following these steps, you can effectively show a loading indicator while waiting for your file to load and then proceed with displaying your action dialog.