0

我正在检查邮递员并获取 Get url 响应是更新成功。但是我在我的项目中使用了 Flutter Http 和 Dio 库。获取响应不更新。http 显示上一个响应。我不明白这是什么问题。为什么 http 和 dio 没有得到方法更新的响应。

https://github.com/shakeelsaifi-it/Flutter-App

检查源代码

4

2 回答 2

0

API 响应存在颤振问题(dio 或 http)

您的状态正在返回一个布尔值并且颤振(dio 或 http)无法对其进行解码。

请将状态从 bool 更改为 int 或 string,如下所述

更改您的 API

{
"status": true,
"data": [
    {
        "event_id": "9",
        "location": "Delhi, India",
        "title": "test",
        "start_time": "2020-07-02 02:08:20",
        "end_time": "2020-07-30 02:08:20",
        "interest": "test theme",
        "interest_desp": "test theme",
        "image": "5f06d7a4a53d3.jpg",
        "comment": "about test",
        "latitude": "28.6862738",
        "longitude": "77.2217831",
        "is_subs": 0
    }
]

}

{
"status": "true",
"data": [
    {
        "event_id": "9",
        "location": "Delhi, India",
        "title": "test",
        "start_time": "2020-07-02 02:08:20",
        "end_time": "2020-07-30 02:08:20",
        "interest": "test theme",
        "interest_desp": "test theme",
        "image": "5f06d7a4a53d3.jpg",
        "comment": "about test",
        "latitude": "28.6862738",
        "longitude": "77.2217831",
        "is_subs": 0
    }
]

}

或者

{
"status": 200,
"data": [
    {
        "event_id": "9",
        "location": "Delhi, India",
        "title": "test",
        "start_time": "2020-07-02 02:08:20",
        "end_time": "2020-07-30 02:08:20",
        "interest": "test theme",
        "interest_desp": "test theme",
        "image": "5f06d7a4a53d3.jpg",
        "comment": "about test",
        "latitude": "28.6862738",
        "longitude": "77.2217831",
        "is_subs": 0
    }
]

}

这将解决您的问题

在颤动中获取响应的代码

未经测试,但我认为它会工作

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';

class Solution extends StatefulWidget {
  @override
  _SolutionState createState() => _SolutionState();
}

class _SolutionState extends State<Solution> {
  Dio dio = new Dio();

  bool isLoading = false;

  List<DataModelClass> _dataList = [];

  Future getData() async {
    setState(() {
      isLoading = true;
    });

    var url =
        'your api provided by you in your git repo';

    var response = await dio.get(url);

    var body = json.decode(response.data);

    if (response.statusCode == 200) {
      for (var item in body['data']) {
        DataModelClass data = new DataModelClass.fromJson(item);
        _dataList.add(data);
      }
      setState(() {
        isLoading = false;
      });
    }
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
    );
  }
}

// model calss


class DataModelClass {
  String eventId;
  String location;
  String title;
  String startTime;
  String endTime;
  String interest;
  String interestDesp;
  String image;
  String comment;
  String latitude;
  String longitude;
  int isSubs;

  DataModelClass(
      {this.eventId,
      this.location,
      this.title,
      this.startTime,
      this.endTime,
      this.interest,
      this.interestDesp,
      this.image,
      this.comment,
      this.latitude,
      this.longitude,
      this.isSubs});

  DataModelClass.fromJson(Map<String, dynamic> json) {
    eventId = json['event_id'];
    location = json['location'];
    title = json['title'];
    startTime = json['start_time'];
    endTime = json['end_time'];
    interest = json['interest'];
    interestDesp = json['interest_desp'];
    image = json['image'];
    comment = json['comment'];
    latitude = json['latitude'];
    longitude = json['longitude'];
    isSubs = json['is_subs'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['event_id'] = this.eventId;
    data['location'] = this.location;
    data['title'] = this.title;
    data['start_time'] = this.startTime;
    data['end_time'] = this.endTime;
    data['interest'] = this.interest;
    data['interest_desp'] = this.interestDesp;
    data['image'] = this.image;
    data['comment'] = this.comment;
    data['latitude'] = this.latitude;
    data['longitude'] = this.longitude;
    data['is_subs'] = this.isSubs;
    return data;
  }
}
于 2020-07-11T06:59:03.737 回答
0
Add Header no cache
Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      "Cache-Control": "no-cache"
    };
于 2020-07-13T07:14:40.420 回答