Created with <3 with dartpad.dev.
Created
May 2, 2023 13:21
-
-
Save hongsw/ab39d63be5437ebf2b69f6c1686ba1ca to your computer and use it in GitHub Desktop.
bold-oak-7479
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Async Future Example', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Async Future Example'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
String _result = 'Loading...'; | |
@override | |
void initState() { | |
super.initState(); | |
_fetchData().then((value) { | |
setState(() { | |
_result = value; | |
}); | |
}); | |
} | |
Future<String> _fetchData() async { | |
await Future.delayed(Duration(seconds: 2)); | |
return 'Hello, World! 🐢'; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Result:', | |
), | |
Text( | |
'$_result', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment