Created
August 27, 2019 13:36
-
-
Save ariG23498/7421ec2c93e337599e3691412e8a7b03 to your computer and use it in GitHub Desktop.
HTTP Flutter Template
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'; | |
import 'package:http/http.dart' as http; | |
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/foundation.dart'; | |
import 'Todo.dart'; | |
Future<List<Todo>> fetchTodos(http.Client client, BuildContext context) async { | |
final response = | |
await client.get('https://jsonplaceholder.typicode.com/todos'); | |
if (response.statusCode == 200) { | |
// If server returns an OK response, parse the JSON. | |
return compute(parseTodos, response.body); | |
} else { | |
// If that response was not OK, throw an error. | |
throw Exception('Failed to load post'); | |
} | |
} | |
List<Todo> parseTodos(String responseBody) { | |
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>(); | |
return parsed.map<Todo>((json) => Todo.fromJson(json)).toList(); | |
} | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'http get', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
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> { | |
List<Todo> todos; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: ListView.builder( | |
itemCount: todos == null ? 0 : todos.length, | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: new Text(todos[index].title), | |
leading: new Text('${todos[index].userId}'), | |
subtitle: new Text('${todos[index].id}'), | |
trailing: new Text('${todos[index].completed}'), | |
); | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.list), | |
onPressed: () async { | |
todos = await fetchTodos(http.Client(), context); | |
setState(() {}); | |
}, | |
), | |
); | |
} | |
} | |
class TodosList extends StatelessWidget { | |
final List<Todo> todos; | |
TodosList({Key key, this.todos}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: todos.length, | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: new Text(todos[index].title), | |
leading: new Text('${todos[index].userId}'), | |
subtitle: new Text('${todos[index].id}'), | |
trailing: new Text('${todos[index].completed}'), | |
); | |
}, | |
); | |
} | |
} |
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
class Todo { | |
int userId; | |
int id; | |
String title; | |
bool completed; | |
// Sugar syntax | |
Todo({this.userId, this.id, this.title, this.completed}); | |
factory Todo.fromJson(Map<String, dynamic> json) { | |
return Todo( | |
userId: json['userId'], | |
id: json['id'], | |
title: json['title'], | |
completed: json['completed'], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment