Flutter Live Data Parsing in Widget

Add following dependency in pubspec.yaml file:

http: ^0.13.1

live.dart

  import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(MyAppDemo());
}

class MyAppDemo extends StatefulWidget {
  @override
  _MyAppDemoState createState() => _MyAppDemoState();
}

class _MyAppDemoState extends State<MyAppDemo> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "",
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
   
    return Scaffold(
      appBar: AppBar(
        title: Text("Live Data Calling"),
      ),
      
      body: Container(
        child: FutureBuilder(
        
          future: getData(),
          builder: (context, snapshot) {
          
            if (snapshot.hasError) {
              return Text("Error");
            } else if(snapshot.hasData){
              if(snapshot.data.length>0){
                return ListView.builder(
                  itemBuilder: (context, index) {
                    User user = snapshot.data[index];
                    return ListTile(
                      title: Text(user.name),
                      subtitle: Text(user.company.name),
                      leading: Text(user.id.toString()),
                      trailing: Row(
                          mainAxisSize: MainAxisSize.min,
                        children: [
                          Text(user.address.city),
                          Text(user.address.geo.lat),
                        ],
                        
                      ),
                    );
                    
                  },
                  
                  itemCount: snapshot.data.length,
                );
                
              }
              else{
                return Text("There is No data");
              }

            }else{
              return Center(child: CircularProgressIndicator());
            }

          },
        ),
      ),
    );
  }

  Future<List<User>> getData() async {
  var resp =
  await http.get(Uri.parse("https://jsonplaceholder.typicode.com/users"));
  var userObjsJson = jsonDecode(resp.body) as List;
  List<User> userObjs =
  userObjsJson.map((userJson) => User.fromJson(userJson)).toList();
  return userObjs;
  }
  
}

class User {
  int id;
  String name;
  Company company;
  Address address;

  User(this.id, this.name, this.company,this.address);

  User.fromMap(Map<String, dynamic> map)
      : id = map['id'],
        name = map['name'],
        company = map['company'],
        address = map['address'];

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'company':company,
      'address':address,
    };
  }

  factory User.fromJson(dynamic json) {
    return User(json['id'] as int,
    json['name'] as String,Company.fromJson(json['company']),
    Address.fromJson(json['address']));
  }
  
}

class Company {
  String name;

  Company(this.name);

  factory Company.fromJson(dynamic json) {
    return Company(json['name'] as String);
  }
}

class Address{
  String street;
  String city;
  Geo geo;
  Address(this.street,this.city,this.geo);

  factory Address.fromJson(dynamic json) {
    return Address(json['street'] as String,
    json['city'] as String,
    Geo.fromJson(json['geo']));
  }
}

class Geo{
  String lat;
  String lng;
  Geo(this.lat,this.lng);

  factory Geo.fromJson(dynamic json) {
    return Geo(json['lat'] as String,json['lng'] as String);
  }
}
  

No comments: