Skip to content

Instantly share code, notes, and snippets.

@erickzanardo
Created January 10, 2022 20:51
Show Gist options
  • Save erickzanardo/c67e7cb6707af33b0bd11e3b7d9b95d0 to your computer and use it in GitHub Desktop.
Save erickzanardo/c67e7cb6707af33b0bd11e3b7d9b95d0 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: Home());
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
var _switchValue = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Switch(
value: _switchValue,
onChanged: (newValue) {
setState(() => _switchValue = newValue);
},
),
FutureBuilder(
future: _fetchData(),
builder: (context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
return Center(child: Text(snapshot.data));
}
},
),
],
),
);
}
Future<String> _fetchData() async {
await Future.delayed(Duration(seconds: 2));
return 'Some Asynchronous Data!';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment