Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3.  
  4. import 'package:flutter/material.dart';
  5. import 'package:http/http.dart' as http;
  6.  
  7. Future<RandomUser> fetchUser() async {
  8. final response = await http
  9. .get('https://randomuser.me/api/?results=20&format=pretty&nat=DK');
  10.  
  11. if (response.statusCode == 200) {
  12. // If the server did return a 200 OK response, then parse the JSON.
  13. return RandomUser.fromJson(json.decode(response.body));
  14. } else {
  15. // If the server did not return a 200 OK response, then throw an exception.
  16. throw Exception('Failed to load user');
  17. }
  18. }
  19.  
  20. class RandomUser {
  21. List<Results> users;
  22.  
  23. RandomUser({this.users});
  24.  
  25. RandomUser.fromJson(Map<String, dynamic> json) {
  26. if (json['results'] != null) {
  27. users = new List<Results>();
  28. json['results'].forEach((v) {
  29.  
  30. users.add(new Results.fromJson(v));
  31. print(v);
  32. });
  33. }
  34. }
  35. List<Results> get getUser => users;
  36. }
  37.  
  38. class Results {
  39. Name name;
  40. Location location;
  41. Picture picture;
  42.  
  43. Results({this.name, this.location, this.picture});
  44.  
  45. Results.fromJson(Map<String, dynamic> json) {
  46. name = json['name'] != null ? new Name.fromJson(json['name']) : null;
  47. location = json['location'] != null
  48. ? new Location.fromJson(json['location'])
  49. : null;
  50. picture =
  51. json['picture'] != null ? new Picture.fromJson(json['picture']) : null;
  52. }
  53. }
  54.  
  55. class Name {
  56. String title;
  57. String first;
  58. String last;
  59.  
  60. Name({this.title, this.first, this.last});
  61.  
  62. Name.fromJson(Map<String, dynamic> json) {
  63. title = json['title'];
  64. first = json['first'];
  65. last = json['last'];
  66. }
  67. }
  68.  
  69. class Location {
  70. Street street;
  71. Location({this.street});
  72.  
  73. Location.fromJson(Map<String, dynamic> json) {
  74. street =
  75. json['street'] != null ? new Street.fromJson(json['street']) : null;
  76. }
  77. }
  78.  
  79. class Street {
  80. int number;
  81. String name;
  82.  
  83. Street({this.number, this.name});
  84.  
  85. Street.fromJson(Map<String, dynamic> json) {
  86. number = json['number'];
  87. name = json['name'];
  88. }
  89.  
  90. Map<String, dynamic> toJson() {
  91. final Map<String, dynamic> data = new Map<String, dynamic>();
  92. data['number'] = this.number;
  93. data['name'] = this.name;
  94. return data;
  95. }
  96. }
  97.  
  98. class Picture {
  99. String large;
  100. String medium;
  101. String thumbnail;
  102.  
  103. Picture({this.large, this.medium, this.thumbnail});
  104.  
  105. Picture.fromJson(Map<String, dynamic> json) {
  106. large = json['large'];
  107. medium = json['medium'];
  108. thumbnail = json['thumbnail'];
  109. }
  110. }
  111.  
  112. class User {
  113. String name;
  114. String picture;
  115. }
  116.  
  117. void main() => runApp(MyApp());
  118.  
  119. class MyApp extends StatefulWidget {
  120. MyApp({Key key}) : super(key: key);
  121.  
  122. @override
  123. _MyAppState createState() => _MyAppState();
  124. }
  125.  
  126. class _MyAppState extends State<MyApp> {
  127. Future<RandomUser> futureUser;
  128.  
  129. @override
  130. void initState() {
  131. super.initState();
  132. futureUser = fetchUser();
  133. }
  134.  
  135. @override
  136. Widget build(BuildContext context) {
  137. return MaterialApp(
  138. title: 'Fetch Data Example',
  139. theme: ThemeData(
  140. primarySwatch: Colors.blue,
  141. ),
  142. home: Scaffold(
  143. appBar: AppBar(
  144. title: Text('Fetch Data Example'),
  145. ),
  146. body: Center(
  147. child: FutureBuilder<RandomUser>(
  148. future: futureUser,
  149. builder: (context, snapshot) {
  150. if (snapshot.hasData) {
  151. return ListView.builder(
  152. padding: const EdgeInsets.all(8),
  153. itemCount: 20,
  154. itemBuilder: (BuildContext context, int index) {
  155. print(snapshot.data.users[index].name.first);
  156. return Column(
  157. children: <Widget>[
  158. Text(snapshot.data.users[index].name.first +
  159. " " +
  160. snapshot.data.users[index].name.last),
  161. Image.network(
  162. snapshot.data.users[index].picture.large)
  163. ],
  164. );
  165. },
  166. );
  167. } else if (snapshot.hasError) {
  168. return Text("${snapshot.error}");
  169. }
  170.  
  171. // By default, show a loading spinner.
  172. return CircularProgressIndicator();
  173. })),
  174. ),
  175. );
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement