Skip to content

Instantly share code, notes, and snippets.

@asavchuk
Last active January 1, 2020 19:26
Show Gist options
  • Save asavchuk/ef79325a4c082df52b65cf66cf2700ed to your computer and use it in GitHub Desktop.
Save asavchuk/ef79325a4c082df52b65cf66cf2700ed to your computer and use it in GitHub Desktop.
FUTURE API 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: 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