Flutter获取Json数据并保存

测试API : https://jsonplaceholder.typicode.com/users

返回格式示例:

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "Shanna@melissa.tv",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  },
  {
    "id": 3,
    "name": "Clementine Bauch",
    "username": "Samantha",
    "email": "Nathan@yesenia.net",
    "address": {
      "street": "Douglas Extension",
      "suite": "Suite 847",
      "city": "McKenziehaven",
      "zipcode": "59590-4157",
      "geo": {
        "lat": "-68.6102",
        "lng": "-47.0653"
      }
    },
    "phone": "1-463-123-4447",
    "website": "ramiro.info",
    "company": {
      "name": "Romaguera-Jacobson",
      "catchPhrase": "Face to face bifurcated interface",
      "bs": "e-enable strategic applications"
    }
  },

用到的第三方库 http: ^0.12.0+2


定义model用于保存数据

class UsersModel {
  int id;
  String name;
  String userName;
  String email;
  String phone;

  UsersModel(
    this.id,
    this.name,
    this.userName,
    this.email,
    this.phone,
  );

  UsersModel.fromJson(Map json)
      : id = json['id'],
        name = json['name'],
        userName = json['username'],
        email = json['email'],
        phone = json['phone'];

  Map toJson() {
    return {
      'id': id,
      'name': name,
      'username': userName,
      'email' : email,
      'phone' : phone,
    };
  }
}

请求方法定义

import 'dart:async';
import 'package:http/http.dart' as http;

const api_v1 = "https://jsonplaceholder.typicode.com";

class UsersList {
  static Future getActiveUsers() {
    var urlUsrList = api_v1 + "/users";
    return http.get(urlUsrList);
  }
}

调用方法

  var activeUsers = new List<UsersModel>();

  Future _getFromApi() async {
    UsersList.getActiveUsers().then((response) {
      setState(() {
        Iterable list = json.decode(response.body);
        activeUsers = list.map((model) => UsersModel.fromJson(model)).toList();
      });
    });
  }

相关文章

这篇文章主要讲解了“FlutterComponent动画的显和隐怎么实现...
这篇文章主要讲解了“flutter微信聊天输入框功能如何实现”,...
本篇内容介绍了“Flutter之Navigator的高级用法有哪些”的有...
这篇文章主要介绍“Flutter怎么使用Android原生播放器”,在...
Flutter开发的android端如何修改APP名称,logo,版本号,具体...
Flutter路由管理初识路由概念一.路由管理1.1.Route1.2.Mater...