Created
September 7, 2020 14:20
-
-
Save shootacean/c8b18dea008fbb4e0f099b7af18f7416 to your computer and use it in GitHub Desktop.
Flutter - HTTP Request
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 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final TextEditingController _controller = TextEditingController(); | |
Future<Response> futureResponse; | |
Future<Response> _futureResponse; | |
@override | |
void initState() { | |
super.initState(); | |
futureResponse = fetchText(); | |
} | |
Future<Response> fetchText() async { | |
final response = await http.get('http://localhost:8080'); | |
if (response.statusCode == 200) { | |
return Response.fromJson(json.decode(response.body)); | |
} else { | |
throw Exception('Failed to load album'); | |
} | |
} | |
Future<Response> updateText(String text) async { | |
final http.Response response = await http.post( | |
'http://localhost:8080/post', | |
headers: <String, String>{ | |
'Content-Type': 'application/json; charset=UTF-8', | |
}, | |
body: jsonEncode(<String, String>{ | |
'text': text, | |
}), | |
); | |
if (response.statusCode == 200) { | |
return Response.fromJson(json.decode(response.body)); | |
} else { | |
throw Exception('Failed to load text'); | |
} | |
} | |
FutureBuilder<Response> buildFuture() { | |
return FutureBuilder<Response>( | |
future: (_futureResponse == null) ? futureResponse : _futureResponse, | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return Text(snapshot.data.text); | |
} else if (snapshot.hasError) { | |
return Text("${snapshot.error}"); | |
} | |
return CircularProgressIndicator(); | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
buildFuture(), | |
TextField( | |
controller: _controller, | |
), | |
RaisedButton( | |
child: Text("Update Text"), | |
onPressed: () { | |
setState(() { | |
_futureResponse = updateText(_controller.text); | |
}); | |
}, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class Response { | |
final String text; | |
Response({this.text}); | |
factory Response.fromJson(Map<String, dynamic> json) { | |
return Response( | |
text: json['text'], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment