【问题标题】:how save data from API - flutter如何从 API 保存数据 - 颤振
【发布时间】:2021-05-20 14:46:53
【问题描述】:

我正在处理 API 请求。我想保存从 API 接收到的所有数据,并在第二种方法中将其作为参数重用。

API 响应:

{
    "code": 0,
    "message": " success",
    "data": {
        "data": [
            {
                "id": 14,
                "boxIdentifiant": 1924589682265255,
                "user_id": 53,
                "boxName": "box12",
                "proprietaire": 21625147147,
                "adress_circulation": "Tokyo",
                "gps_lat": null,
                "gps_long": null,
                "status": "normal"
            }
        ]
    },
    "error": [],
    "status": 200
}

我通过这种方法接收到上面的数据:

Future<UserBox> fetchBoxes() async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    String token = localStorage.getString('access_token');
    await checkInternet();
    Map<String, String> headers = {
      'Content-type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token'
    };
    var url = Uri.parse(ApiUtil.GET_ALL_BOXES);
    var response = await http.get(url, headers: headers);
    var body = jsonDecode(response.body);
    var data = body['data']['data'];

    List<BoxModel> boxes =
        List.generate(data.length, (index) => BoxModel.fromJson(data[index]));
    final userbox = UserBox()..boxes = boxes;
    return userbox;
  }

如何保存所有数据?

【问题讨论】:

    标签: api flutter dart


    【解决方案1】:
    UserBox data = await fetchBoxes();
    secondMethod(data);
    

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2020-11-18
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 2020-09-19
      • 1970-01-01
      • 2019-07-01
      相关资源
      最近更新 更多