Created
September 16, 2020 14:17
-
-
Save h4p/99c4df1dcb397d92a5246e0fc5e1088f to your computer and use it in GitHub Desktop.
Flutter - Async Method Call
This file contains 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'; | |
void main() => runApp(MaterialApp(home: Scaffold(body: Center(child: MyWidget())))); | |
Future<String> callAsyncFetch() => Future.delayed(Duration(seconds: 2), () => "hi"); | |
class MyWidget extends StatelessWidget { | |
@override | |
Widget build(context) { | |
return FutureBuilder<String>( | |
future: callAsyncFetch(), | |
builder: (context, AsyncSnapshot<String> snapshot) { | |
if (snapshot.hasData) { | |
return Text(snapshot.data); | |
} else { | |
return CircularProgressIndicator(); | |
} | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment