Skip to content

Instantly share code, notes, and snippets.

@asavchuk
Last active January 1, 2020 19:26
Show Gist options
  • Save asavchuk/7ed7cd634c494454d5fcad5721926743 to your computer and use it in GitHub Desktop.
Save asavchuk/7ed7cd634c494454d5fcad5721926743 to your computer and use it in GitHub Desktop.
ASYNC AND AWAIT IN FLUTTER
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: AsyncAwait(),
);
}
}
class AsyncAwait extends StatefulWidget {
@override
_AsyncAwaitState createState() => _AsyncAwaitState();
}
class _AsyncAwaitState extends State<AsyncAwait> {
bool _isLoading = false;
void _asyncAction() async {
setState(() => _isLoading = true);
await Future.delayed(Duration(seconds: 5));
setState(() => _isLoading = false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Async and Await example")),
body: Center(
child: _isLoading
? CircularProgressIndicator()
: RaisedButton(
child: Text("Start Asyncronous action"),
color: Colors.red,
onPressed: _asyncAction,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment