Skip to content

Instantly share code, notes, and snippets.

@gabrielgatu
Last active November 22, 2021 07:14
Show Gist options
  • Save gabrielgatu/67b76749e8cb92cab6230e64982f2db0 to your computer and use it in GitHub Desktop.
Save gabrielgatu/67b76749e8cb92cab6230e64982f2db0 to your computer and use it in GitHub Desktop.
Flutter2Start - FutureBuilder #flutter2start
// ignore_for_file: use_key_in_widget_constructors, prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late Future<http.Response> httpRequestToGoogle;
@override
void initState() {
super.initState();
setState(() {
httpRequestToGoogle = http.get(Uri.parse("https://edu.fudeo.it/"));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter2Start"),
centerTitle: true,
),
body: Center(child: body()),
);
}
Widget body() => FutureBuilder<http.Response>(
future: httpRequestToGoogle,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
else {
return Text(snapshot.data!.body);
}
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment