Ad
How To Show A Widget While A Asyncronous Task Is Executing And Then Show A Done Widget After It Has Executed In Flutter
I wanted to make a asynchronous loading dialog, that retrieves data from a database(this is the asynchronous task) and once it is done, i want to show a dialog box that shows the data retrieved. Meaning, I want to show a loading dialog box widget while the data is being retrieved from the database, and once the data is retrieved, show it on the screen using a dialog box
Ad
Answer
In this we need to handle tree scenario,
- Before data call, show nothing
- on future call, show progressBar
- after fetch, use data
Using two nullable variable to handle this situation
Simply replace the widget according to your need.
class _WelcomeScreenState extends State<WelcomeScreen> {
Future<int> fecthData() async {
return Future.delayed(
Duration(seconds: 3),
).then(
(value) => 4,
);
}
bool? _isLoading;
int? data;
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) => Column(
children: [
ElevatedButton(
onPressed: () async {
setState(() {
_isLoading = true; //set true while fetching
});
data = await fecthData();
setState(() {
_isLoading = false; //set false while fetching
});
},
child: Text("Fetch")),
if (_isLoading == true) CircularProgressIndicator(),
if (data != null) Text("${data!}") // if you have data show it
],
),
),
);
}
}
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad