Last active
November 22, 2021 07:15
-
-
Save gabrielgatu/bae0f5d9b7c0a8c7d669291d53294600 to your computer and use it in GitHub Desktop.
Flutter2Start - http initState & setState #flutter2start
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
// 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> { | |
String responseBody = ""; | |
bool isLoading = false; | |
@override | |
void initState() { | |
super.initState(); | |
startMakingRequest(); | |
} | |
void startMakingRequest() async { | |
setState(() { | |
isLoading = true; | |
}); | |
final url = Uri.parse("https://edu.fudeo.it/"); | |
final response = await http.get(url); | |
setState(() { | |
isLoading = false; | |
}); | |
if (response.statusCode == 200) { | |
setState(() { | |
responseBody = response.body; | |
}); | |
} else { | |
setState(() { | |
responseBody = "ERROR"; | |
}); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Flutter2Start"), | |
centerTitle: true, | |
), | |
body: Center(child: body()), | |
); | |
} | |
Widget body() => isLoading ? SizedBox(width: 40, height: 40, child: CircularProgressIndicator()) : Text(responseBody); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment