Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 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 =
  9. await http.get('https://randomuser.me/api/?results=&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. users.add(new Results.fromJson(v));
  30. });
  31. }
  32. }
  33. }
  34.  
  35. class Results {
  36. Name name;
  37. Location location;
  38. Picture picture;
  39.  
  40. Results(
  41. {
  42. this.name,
  43. this.location,
  44. this.picture});
  45.  
  46. Results.fromJson(Map<String, dynamic> json) {
  47. name = json['name'] != null ? new Name.fromJson(json['name']) : null;
  48. location = json['location'] != null
  49. ? new Location.fromJson(json['location'])
  50. : null;
  51. picture =
  52. json['picture'] != null ? new Picture.fromJson(json['picture']) : null;
  53. }
  54. }
  55.  
  56. class Name {
  57. String title;
  58. String first;
  59. String last;
  60.  
  61. Name({this.title, this.first, this.last});
  62.  
  63. Name.fromJson(Map<String, dynamic> json) {
  64. title = json['title'];
  65. first = json['first'];
  66. last = json['last'];
  67. }
  68. }
  69.  
  70. class Location {
  71. Street street;
  72. Location(
  73. {this.street});
  74.  
  75. Location.fromJson(Map<String, dynamic> json) {
  76. street =
  77. json['street'] != null ? new Street.fromJson(json['street']) : null;
  78. }
  79. }
  80.  
  81. class Street {
  82. int number;
  83. String name;
  84.  
  85. Street({this.number, this.name});
  86.  
  87. Street.fromJson(Map<String, dynamic> json) {
  88. number = json['number'];
  89. name = json['name'];
  90. }
  91.  
  92. Map<String, dynamic> toJson() {
  93. final Map<String, dynamic> data = new Map<String, dynamic>();
  94. data['number'] = this.number;
  95. data['name'] = this.name;
  96. return data;
  97. }
  98. }
  99.  
  100. class Picture {
  101. String large;
  102. String medium;
  103. String thumbnail;
  104.  
  105. Picture({this.large, this.medium, this.thumbnail});
  106.  
  107. Picture.fromJson(Map<String, dynamic> json) {
  108. large = json['large'];
  109. medium = json['medium'];
  110. thumbnail = json['thumbnail'];
  111. }
  112. }
  113.  
  114. void main() => runApp(MyApp());
  115.  
  116. class MyApp extends StatefulWidget {
  117. MyApp({Key key}) : super(key: key);
  118.  
  119. @override
  120. _MyAppState createState() => _MyAppState();
  121. }
  122.  
  123. class _MyAppState extends State<MyApp> {
  124. Future<RandomUser> futureUser;
  125.  
  126. @override
  127. void initState() {
  128. super.initState();
  129. futureUser = fetchUser();
  130. }
  131.  
  132. @override
  133. Widget build(BuildContext context) {
  134. return MaterialApp(
  135. title: 'Fetch Data Example',
  136. theme: ThemeData(
  137. primarySwatch: Colors.blue,
  138. ),
  139. home: Scaffold(
  140. appBar: AppBar(
  141. title: Text('Fetch Data Example'),
  142. ),
  143. body: Center(
  144. child: FutureBuilder<RandomUser>(
  145. future: futureUser,
  146. builder: (context, snapshot) {
  147. if (snapshot.hasData) {
  148. print(snapshot.data.users[0].name.first);
  149. return Column(
  150. children: <Widget>[
  151. Text(snapshot.data.users[0].name.first + snapshot.data.users[0].name.last),
  152. Image.network(snapshot.data.users[0].picture.large)
  153. ],
  154. );
  155.  
  156. } else if (snapshot.hasError) {
  157. return Text("${snapshot.error}");
  158. }
  159.  
  160. // By default, show a loading spinner.
  161. return CircularProgressIndicator();
  162. },
  163. ),
  164. ),
  165. ),
  166. );
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement