Last active
January 1, 2020 19:26
-
-
Save asavchuk/ef79325a4c082df52b65cf66cf2700ed to your computer and use it in GitHub Desktop.
FUTURE API 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: FutureInFlutter(), | |
); | |
} | |
} | |
class FutureInFlutter extends StatefulWidget { | |
@override | |
_FutureInFlutterState createState() => _FutureInFlutterState(); | |
} | |
class _FutureInFlutterState extends State<FutureInFlutter> { | |
bool _isLoading = false; | |
@override | |
void initState() { | |
print('initState()'); | |
super.initState(); | |
initApp(); | |
} | |
initApp() async { | |
print('initApp()'); | |
setState(() => _isLoading = true); | |
Future.delayed(Duration(seconds: 6)).then((_) { | |
print('setState()'); | |
setState(() => _isLoading = false); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print('build()'); | |
return Scaffold( | |
appBar: AppBar(title: Text("Future Example")), | |
body: Center( | |
child: _isLoading | |
? CircularProgressIndicator() | |
: Text("Future Completed!", | |
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold))), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment