Created
January 11, 2020 14:32
-
-
Save maffan91/5c2f6b1752bd06b48bcde2c834101531 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'; | |
class UserList extends StatelessWidget{ | |
final String apiUrl = "https://randomuser.me/api/?results=10"; | |
Future<List<dynamic>> fetchUsers() async { | |
var result = await http.get(apiUrl); | |
return json.decode(result.body)['results']; | |
} | |
String _name(dynamic user){ | |
return user['name']['title'] + " " + user['name']['first'] + " " + user['name']['last']; | |
} | |
String _location(dynamic user){ | |
return user['location']['country']; | |
} | |
String _age(Map<dynamic, dynamic> user){ | |
return "Age: " + user['dob']['age'].toString(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('User List'), | |
), | |
body: Container( | |
child: FutureBuilder<List<dynamic>>( | |
future: fetchUsers(), | |
builder: (BuildContext context, AsyncSnapshot snapshot) { | |
if(snapshot.hasData){ | |
print(_age(snapshot.data[0])); | |
return ListView.builder( | |
padding: EdgeInsets.all(8), | |
itemCount: snapshot.data.length, | |
itemBuilder: (BuildContext context, int index){ | |
return | |
Card( | |
child: Column( | |
children: <Widget>[ | |
ListTile( | |
leading: CircleAvatar( | |
radius: 30, | |
backgroundImage: NetworkImage(snapshot.data[index]['picture']['large'])), | |
title: Text(_name(snapshot.data[index])), | |
subtitle: Text(_location(snapshot.data[index])), | |
trailing: Text(_age(snapshot.data[index])), | |
) | |
], | |
), | |
); | |
}); | |
}else { | |
return Center(child: CircularProgressIndicator()); | |
} | |
}, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment