dart 如何修复API响应中flutter丢失的关联数组键?

cgvd09ve  于 2023-03-21  发布在  Flutter
关注(0)|答案(1)|浏览(166)

bounty将在5天后过期。回答此问题可获得+100的声誉奖励。Atlas-Pio正在寻找来自声誉良好的来源的答案

我有一个使用Laravel创建的Back-end,当我收到API请求的响应时,带有URL的密钥将被丢弃/丢失。
我已经用Front-endPostman等测试过了,但是当我在Flutter应用程序中得到响应时,键会丢失。
下面是一个正确回答的例子,我在Front-endPostman等中得到:

"businessLinks": [
   {
       "linkTitle": "dsadasdasd",
       "linkUrl": "https://example.com/examples/7465f64a-b55b-4a2c-99da-69595a430694/detail" 
   }
],

下面是在Flutter中调用相同API请求时所得到的示例

"businessLinks": [
   {
       "linkTitle": "dsadasdasd",
       "https://example.com/examples/7465f64a-b55b-4a2c-99da-69595a430694/detail" 
   }
],

它总是丢弃URL的键,我已经在其他请求中测试过了,同样的事情再次发生。
唯一能保存键的方法就是用数组来获取键,但值是一个包含一个链接的数组.
另一件事是,我没有打印以上响应,它们是在Android studiorun选项卡中请求API调用时打印的,所以我没有参与其中,这只是Retrofit的行为。
Flutter设置如下:
一个一个二个一个一个一个三个一个一个一个一个一个四个一个
我可以分享更多的代码,如果需要的话,但我没有,因为它是MVVM DDD DI REPOSITORY项目,所以写了很多代码,它可能会得到问题与充分的代码。
上面的代码是工作,我可以得到我的数据,我可以显示数据在用户界面,但唯一的问题是下降/失踪的关键。
项目DNA:

retrofit: ^3.3.1
dio: ^4.0.6
analyzer: ^4.7.0
json_serializable: ^6.1.5
get_it: ^7.2.0

retrofit_generator: ^4.2.0
build_runner: ^2.1.8
flutter_launcher_icons: "^0.11.0"

Flutter 3.3.5 • channel stable
Tools • Dart 2.18.2 • DevTools 2.15.0

编辑01:
以下是回应:

@JsonSerializable()
class ExampleDetailResponse {
  @JsonKey(name: "exampleName")
  String exampleName;

  @JsonKey(name: "exampleLinks")
  List<ExamplLinksResponse> exampleLinks;

  @JsonKey(name: "exampleBanners")
  ImageDataResponse exampleBanners;

  ExampleDetailResponse (this.businessName,
      this.businessLinks,
      this.businessBanner);

  Map<String, dynamic> toJson() => _$ExampleDetailResponseToJson(this);

  factory ExampleDetailResponse.fromJson(Map<String, dynamic> json) => _$ExampleDetailResponseFromJson(json);
}

@JsonSerializable()
class ExamplLinksResponse{
  @JsonKey(name: "linkTitle")
  String linkTitle;
  @JsonKey(name: "linkUrl")
  String linkUrl;

  ExamplLinksResponse(this.linkTitle, this.linkUrl);

  Map<String, dynamic> toJson() => _$ExamplLinksResponseToJson(this);

  factory ExamplLinksResponse.fromJson(Map<String, dynamic> json) => _$ExamplLinksResponseFromJson(json);
}

@JsonSerializable()
class ImageDataResponse {
  @JsonKey(name: "low")
  String low;
  @JsonKey(name: "normal")
  String normal;

  ImageDataResponse(this.low, this.normal);

  Map<String, dynamic> toJson() => _$ImageDataResponseToJson(this);

  factory ImageDataResponse.fromJson(Map<String, dynamic> json) => _$ImageDataResponseFromJson(json);
}

更改键名称不会产生任何差异,因为在上面的示例中,ImageData正在变低,并且图像链接正常,两个键都将被丢弃。
更新日期:01
所以这是我面临的问题,我会根据测试更新这个问题:
在正常的URL中,键不会丢失,例如:www.google.com或google.com
但在下面的事情正在发生:

I/flutter ( 7893): ║   "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-898
I/flutter ( 7893): ║   6-ee98adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpe
I/flutter ( 7893): ║   g"

正如你所看到的,它将在URL中间的下一行,现在我将尝试从后端拆分上述URL,以查看前端的结果。
现在我已经将URL的各个部分分离开来,正如你在下面看到的,在示例键之后,其余的键将被删除,这可能是因为uuid或uuid的结构。
后端响应的单独URL:

"completed" => "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg",
"baseURL" => "https://cdn.example.ir",
"publicImages" => "https://cdn.example.ir/public_images",
"examples" => "https://cdn.example.ir/public_images/examples",
"examplesId" => "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8",
"image" => "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/image",
"banner" => "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/image/_banner_",
"bannerId" => "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/image/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low",
"full" => "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg",

=〉导致Flutter

I/flutter ( 7893): ║                 "https://cdn.the-hooray.ir/public_images/businesses/3fb9acda-f377-4cfe-898
I/flutter ( 7893): ║                 6-ee98adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpe
I/flutter ( 7893): ║                 g"
I/flutter ( 7893): ║                 "https://cdn.the-hooray.ir/public_images/businesses/3fb9acda-f377-4cfe-898
I/flutter ( 7893): ║                 6-ee98adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8.jpeg"
I/flutter ( 7893): ║                 "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee9
I/flutter ( 7893): ║                 8adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg"
I/flutter ( 7893): ║                 baseURL: "https://cdn.example.ir",
I/flutter ( 7893): ║                 publicImages: "https://cdn.example.ir/public_images",
I/flutter ( 7893): ║                 examples: "https://cdn.example.ir/public_images/examples",
I/flutter ( 7893): ║                 "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee9
I/flutter ( 7893): ║                 8adf5b9c8"
I/flutter ( 7893): ║                 "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee9
I/flutter ( 7893): ║                 8adf5b9c8/image"
I/flutter ( 7893): ║                 "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee9
I/flutter ( 7893): ║                 8adf5b9c8/image/_banner_"
I/flutter ( 7893): ║                 "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee9
I/flutter ( 7893): ║                 8adf5b9c8/image/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low"
I/flutter ( 7893): ║                 "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee9
I/flutter ( 7893): ║                 8adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg"

Feels like it's separating full links with UUIDs, dropping their keys and etc. It might be the structure, hyphens or...

更新日期02:我已经更新了翻新,dio,和所有与翻新相关的库,仍然没有帮助...
更新03:所以经过多次测试,我发现问题是与URL长度,但仍然没有解决方案,为什么它是下降的关键

https://cdn.example.ir/publicimagespublicimagespublicimages => key dropped
https://cdn.example.ir/publicimagespublicimagespublic => key exist
https://cdn.example.ir/publicimagespublicimagespublicim => key exist
https://cdn.example.ir/publicimagespublicimagespublicima => key dropped
xwbd5t1u

xwbd5t1u1#

我试着阅读了pretty_dio_logger的源代码,发现如果行长度超过行宽度,它会剪切响应。
您可以在www.example.com上找到https://github.com/Milad-Akarie/pretty_dio_logger/blob/f1ab4e4ff39b755c56a38d1daff335bcec0bb28f/lib/pretty_dio_logger.dart#L236
我尝试在我的机器上进行测试,并执行以下操作:

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

void main() async {
  final dio = Dio();
  dio.interceptors.add(PrettyDioLogger());

  final response =
      await dio.get('https://mocki.io/v1/62377d93-7359-42c0-b8cd-c721f62f2501');

  print(
      "======================================================================");
  print("actual response");

  print(response);

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const SizedBox.shrink();
  }
}

输出为

Reload already in progress, ignoring request
Restarted application in 943ms.
╔╣ Request ║ GET
║  https://mocki.io/v1/62377d93-7359-42c0-b8cd-c721f62f2501
╚══════════════════════════════════════════════════════════════════════════════════════════╝
╔╣ Response ║ GET ║ Status: 200
║  https://mocki.io/v1/62377d93-7359-42c0-b8cd-c721f62f2501
╚══════════════════════════════════════════════════════════════════════════════════════════╝
╔ Body
║
║    {
║         "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c
║         8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg"
║         baseURL: "https://cdn.example.ir",
║         publicImages: "https://cdn.example.ir/public_images",
║         examples: "https://cdn.example.ir/public_images/examples",
║         "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c
║         8"
║         "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c
║         8/image"
║         "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c
║         8/image/_banner_"
║         "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c
║         8/image/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low"
║         "https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c
║         8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg"
║    }
║
╚══════════════════════════════════════════════════════════════════════════════════════════╝
======================================================================
actual response
{"completed":"https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg","baseURL":"https://cdn.example.ir","publicImages":"https://cdn.example.ir/public_images","examples":"https://cdn.example.ir/public_images/examples","examplesId":"https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8","image":"https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/image","banner":"https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/image/_banner_","bannerId":"https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/image/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low","full":"https://cdn.example.ir/public_images/examples/3fb9acda-f377-4cfe-8986-ee98adf5b9c8/images/_banner_3fb9acda-f377-4cfe-8986-ee98adf5b9c8_low.jpeg"}

这里你可以看到响应有这些键,但是漂亮的日志没有。如果你在定义PrettyDioLogger的时候增加maxWidth,你会得到漂亮的响应。PrettyDioLogger(maxWidth: 500)

相关问题