Skip to content

Instantly share code, notes, and snippets.

@asavchuk
Last active January 1, 2020 19:26
Show Gist options
  • Save asavchuk/bc2ca60d7702d0bc78217801dff5cdb2 to your computer and use it in GitHub Desktop.
Save asavchuk/bc2ca60d7702d0bc78217801dff5cdb2 to your computer and use it in GitHub Desktop.
Flutter async and await
import 'package:flutter/material.dart';
class SandGlass {
int _sand = 100;
time() {
return _sand;
}
Future timeDecrease() async {
_sand = _sand -10;
await new Future.delayed(const Duration(milliseconds: 500));
print('time_decrease()');
}
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State {
SandGlass clock = SandGlass();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
_reDrawWidget();
print('---------${clock.time()}');
return Center(child: Text('time is: ${clock.time()}'));
}
_reDrawWidget() async {
if (clock.time() == 0) return;
await Future.delayed(const Duration(seconds: 1));
print('reDrawWidget()\n');
clock.timeDecrease();
setState(() {});
}
}
void main() => runApp(new MaterialApp(home: Scaffold(body: MyApp())));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment