Dolphin
Dolphin

Reputation: 38607

why the flutter response received json did not deserialize

I am using dio response to receive json response from server side in flutter, this is part of the code:

  @override
  Future onResponse(Response response, ResponseInterceptorHandler handler) async {
    Response handleResponse = await autoLogin(response);
    Response handleAccessToken = await handleResponseByCode(handleResponse);
    return super.onResponse(handleAccessToken, handler);
  }

sometimes I found the dio Response auto deserialize the json result to _InternalLinkedHashMap, like this:

enter image description here

sometimes are not deserialize by default. Just receive string like this:

enter image description here

what should I do to make all response deserialize by default? add some response header? If the deserialize action not keep the same, the client code sometimes runs fine, sometimes failed. This is the interceptor of my project current using, which I am parse the response and do some AOP logic:

class AppInterceptors extends InterceptorsWrapper {
  @override
  Future<void> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    if (!options.headers.containsKey("accessToken")) {
      String? accessToken = await SecureStorageUtil.getString("accessToken");
      options.headers["accessToken"] = accessToken??"";
    }
    handler.next(options);
  }

  @override
  Future onResponse(Response response, ResponseInterceptorHandler handler) async {
    Response handleResponse = await autoLogin(response);
    Response handleAccessToken = await handleResponseByCode(handleResponse);
    return super.onResponse(handleAccessToken, handler);
  }
}

this is the json data that did not deserialize by default:

{
body: [
{
id: 1,
creator: 1,
name: "Like Music",
cover_url: "",
description: null,
subscribed: 1,
subscribed_count: 1,
comment_count: 1,
share_count: 1,
play_count: 1
}
],
statusCode: "200",
resultCode: "200"
}

BTW, this json data was generate on the server side by using rust serde_json to serialize like this:

#[get("/v1/playlist")]
pub fn user_playlist() -> io::Result<String> {
    use schema::playlist::dsl::*;
    let connection = config::establish_connection();
    let results = playlist.filter(creator.eq(1))
        .limit(1)
        .load::<QueryPlaylist>(&connection)
        .expect("Error loading posts");
    let res = ApiResponse {
        body: results,
        ..Default::default()
    };
    let response_json = serde_json::to_string(&res).unwrap();
    return Ok(response_json);
}

Upvotes: 0

Views: 124

Answers (1)

Caffo17
Caffo17

Reputation: 543

You need to check two things:

  • In the Options of your request have you set the parameter responseType to ResponseType.json?
  • Did the response have the header content-type valued application/json?

Dio set the parameter responseType to ResponseType.json by default only if the response has the header content-type valued application/json. (You can see it on the Dio's doc).

Upvotes: 1

Related Questions