Last active
September 4, 2018 01:00
-
-
Save YoshihitoAso/532c7562889833ef929c2f74db2c9518 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: 'API Fetch Example', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: new Text('API Fetch 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() | |
] | |
); | |
}, | |
padding: const EdgeInsets.all(16.0), | |
); | |
} 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}); | |
} |
This file contains hidden or 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:first_app_api/client/github_api_client.dart'; | |
import 'package:first_app_api/entity/github_user.dart'; | |
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: 'API Fetch Sample', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: new Text('API Fetch Sample'), | |
), | |
body: new Container( | |
child: new FutureBuilder<List<User>>( | |
future: GitHubClient().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() | |
] | |
); | |
}, | |
padding: const EdgeInsets.all(16.0), | |
); | |
} else if (snapshot.hasError) { | |
return new Text("${snapshot.error}"); | |
} | |
// By default, show a loading spinner | |
return new CircularProgressIndicator(); | |
}, | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains hidden or 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:http/http.dart' as http; | |
import 'dart:convert'; | |
import 'dart:async'; | |
import 'package:first_app_api/entity/github_user.dart'; | |
class GitHubClient { | |
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; | |
} | |
} |
This file contains hidden or 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 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