Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- Future<RandomUser> fetchUser() async {
- final response = await http
- .get('https://randomuser.me/api/?results=20&format=pretty&nat=DK');
- if (response.statusCode == 200) {
- // If the server did return a 200 OK response, then parse the JSON.
- return RandomUser.fromJson(json.decode(response.body));
- } else {
- // If the server did not return a 200 OK response, then throw an exception.
- throw Exception('Failed to load user');
- }
- }
- class RandomUser {
- List<Results> users;
- RandomUser({this.users});
- RandomUser.fromJson(Map<String, dynamic> json) {
- if (json['results'] != null) {
- users = new List<Results>();
- json['results'].forEach((v) {
- users.add(new Results.fromJson(v));
- print(v);
- });
- }
- }
- List<Results> get getUser => users;
- }
- class Results {
- Name name;
- Location location;
- Picture picture;
- Results({this.name, this.location, this.picture});
- Results.fromJson(Map<String, dynamic> json) {
- name = json['name'] != null ? new Name.fromJson(json['name']) : null;
- location = json['location'] != null
- ? new Location.fromJson(json['location'])
- : null;
- picture =
- json['picture'] != null ? new Picture.fromJson(json['picture']) : null;
- }
- }
- class Name {
- String title;
- String first;
- String last;
- Name({this.title, this.first, this.last});
- Name.fromJson(Map<String, dynamic> json) {
- title = json['title'];
- first = json['first'];
- last = json['last'];
- }
- }
- class Location {
- Street street;
- Location({this.street});
- Location.fromJson(Map<String, dynamic> json) {
- street =
- json['street'] != null ? new Street.fromJson(json['street']) : null;
- }
- }
- class Street {
- int number;
- String name;
- Street({this.number, this.name});
- Street.fromJson(Map<String, dynamic> json) {
- number = json['number'];
- name = json['name'];
- }
- Map<String, dynamic> toJson() {
- final Map<String, dynamic> data = new Map<String, dynamic>();
- data['number'] = this.number;
- data['name'] = this.name;
- return data;
- }
- }
- class Picture {
- String large;
- String medium;
- String thumbnail;
- Picture({this.large, this.medium, this.thumbnail});
- Picture.fromJson(Map<String, dynamic> json) {
- large = json['large'];
- medium = json['medium'];
- thumbnail = json['thumbnail'];
- }
- }
- class User {
- String name;
- String picture;
- }
- void main() => runApp(MyApp());
- class MyApp extends StatefulWidget {
- MyApp({Key key}) : super(key: key);
- @override
- _MyAppState createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- Future<RandomUser> futureUser;
- @override
- void initState() {
- super.initState();
- futureUser = fetchUser();
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Fetch Data Example',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: Scaffold(
- appBar: AppBar(
- title: Text('Fetch Data Example'),
- ),
- body: Center(
- child: FutureBuilder<RandomUser>(
- future: futureUser,
- builder: (context, snapshot) {
- if (snapshot.hasData) {
- return ListView.builder(
- padding: const EdgeInsets.all(8),
- itemCount: 20,
- itemBuilder: (BuildContext context, int index) {
- print(snapshot.data.users[index].name.first);
- return Column(
- children: <Widget>[
- Text(snapshot.data.users[index].name.first +
- " " +
- snapshot.data.users[index].name.last),
- Image.network(
- snapshot.data.users[index].picture.large)
- ],
- );
- },
- );
- } else if (snapshot.hasError) {
- return Text("${snapshot.error}");
- }
- // By default, show a loading spinner.
- return CircularProgressIndicator();
- })),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement