Last active
January 1, 2020 19:26
-
-
Save asavchuk/7ed7cd634c494454d5fcad5721926743 to your computer and use it in GitHub Desktop.
ASYNC AND AWAIT IN FLUTTER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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