Last active
April 18, 2018 03:17
-
-
Save rajajawahar/4fdb356715528e7bdbd5665f04ac95b5 to your computer and use it in GitHub Desktop.
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:convert'; | |
import 'dart:async'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Fetch Data Example', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Fetch Data Example'), | |
), | |
body: new Container( | |
child: new FutureBuilder<List<User>>( | |
future: fetchUsersFromGitHub(), | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return new ListView.builder( | |
itemCount: snapshot.data.length, | |
itemBuilder: (context, index) { | |
return new Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
new Text(snapshot.data[index].name, | |
style: new TextStyle(fontWeight: FontWeight.bold)), | |
new Divider() | |
] | |
); | |
} | |
); | |
} else if (snapshot.hasError) { | |
return new Text("${snapshot.error}"); | |
} | |
// By default, show a loading spinner | |
return new CircularProgressIndicator(); | |
}, | |
), | |
), | |
), | |
); | |
} | |
Future<List<User>> fetchUsersFromGitHub() async { | |
final response = await http.get('https://api.github.com/users'); | |
print(response.body); | |
List responseJson = json.decode(response.body.toString()); | |
List<User> userList = createUserList(responseJson); | |
return userList; | |
} | |
List<User> createUserList(List data){ | |
List<User> list = new List(); | |
for (int i = 0; i < data.length; i++) { | |
String title = data[i]["login"]; | |
int id = data[i]["id"]; | |
User user = new User( | |
name: title,id: id); | |
list.add(user); | |
} | |
return list; | |
} | |
} | |
class User { | |
String name; | |
int id; | |
User({this.name,this.id}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment