Flutter 转换 Future<对象>映射<字符串 ,动态>

发布于 2025-01-10 02:09:18 字数 5613 浏览 1 评论 0原文

我需要发出 HTTP Post JSON 请求,但首先我需要获取 3 个不同的 JSON 并制作其中 1 个。 我认为更好的方法是使用 Map 对象将它们组合起来,然后发布。 现在我想知道如何将 Future 转换为 Map

这是读取 json 的 main.dart:

import 'dart:convert';
import 'package:api/models/processing.dart';
import 'package:http/http.dart' as http;

main() async {
  fetchProcessing();
}

Future<Processing> fetchProcessing() async {
  final response = await http.get(
      Uri.parse(
          'url'),
      headers: {
        'authorization': 'Bearer token',
        'Content-Type': 'application/json',
        "Access-Control_Allow_Origin": "*",
      });
  if (response.statusCode == 200) { 

    return Processing.fromJson(response.body);
    
  } else {
    print(response.statusCode);
    throw Exception('Failed to load JSON');
  }
}

这是 JSON 的模型类:

import 'dart:convert';

class Processing {
  Processing({
    required this.organization,
    required this.processingSum,
    required this.quantity,
    required this.processingPlan,
    required this.productsStore,
    required this.materialsStore,
    required this.products,
    required this.materials,
  });

  final MaterialsStore organization;
  final double processingSum;
  final double quantity;
  final MaterialsStore processingPlan;
  final MaterialsStore productsStore;
  final MaterialsStore materialsStore;
  final Materials products;
  final Materials materials; 

  factory Processing.fromJson(Map<String, dynamic> json) => Processing(
        organization: MaterialsStore.fromJson(json["organization"]),
        processingSum: json["processingSum"],
        quantity: json["quantity"],
        processingPlan: MaterialsStore.fromJson(json["processingPlan"]),
        productsStore: MaterialsStore.fromJson(json["productsStore"]),
        materialsStore: MaterialsStore.fromJson(json["materialsStore"]),
        products: Materials.fromJson(json["products"]),
        materials: Materials.fromJson(json["materials"]),
      );

  Map<String, dynamic> toJson() => {
        "organization": organization.toJson(),
        "processingSum": processingSum,
        "quantity": quantity,
        "processingPlan": processingPlan.toJson(),
        "productsStore": productsStore.toJson(),
        "materialsStore": materialsStore.toJson(),
        "products": products.toJson(),
        "materials": materials.toJson(),
      };
}

class Materials {
  Materials(); 

  factory Materials.fromJson(Map<String, dynamic> json) => Materials();

  Map<String, dynamic> toJson() => {};
}

class MaterialsStore {
  MaterialsStore({
    required this.meta,
  });

  final Meta meta;  

  factory MaterialsStore.fromJson(Map<String, dynamic> json) => MaterialsStore(
        meta: Meta.fromJson(json["meta"]),
      );

  Map<String, dynamic> toJson() => {
        "meta": meta.toJson(),
      };
}

class Meta {
  Meta({
    required this.href,
    required this.metadataHref,
    required this.type,
    required this.mediaType,
    required this.uuidHref,
  });

  final String href;
  final String metadataHref;
  final String type;
  final String mediaType;
  final String uuidHref;  

  factory Meta.fromJson(Map<String, dynamic> json) => Meta(
        href: json["href"],
        metadataHref: json["metadataHref"],
        type: json["type"],
        mediaType: json["mediaType"],
        uuidHref: json["uuidHref"],
      );

  Map<String, dynamic> toJson() => {
        "href": href,
        "metadataHref": metadataHref,
        "type": type,
        "mediaType": mediaType,
        "uuidHref": uuidHref,
      };
}

这是我正在解析的 JSON:

{   
    "organization" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/organization/5a0f5b7b-a5a7-11ea-0a80-03a2000bb129",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/organization/metadata",
        "type" : "organization",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#mycompany/edit?id=5a0f5b7b-a5a7-11ea-0a80-03a2000bb129"
      }
    },   
    "processingSum" : 10000.0,
    "quantity" : 3.0,
    "processingPlan" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/processingplan/2b0530e5-57e2-11ec-0a80-027e001adc60",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/processingplan/metadata",
        "type" : "processingplan",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#processingplan/edit?id=2b0530e5-57e2-11ec-0a80-027e001adc60"
      }
    },
    "productsStore" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/store/5a10ca61-a5a7-11ea-0a80-03a2000bb12b",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/store/metadata",
        "type" : "store",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#warehouse/edit?id=5a10ca61-a5a7-11ea-0a80-03a2000bb12b"
      }
    },
    "materialsStore" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/store/5a10ca61-a5a7-11ea-0a80-03a2000bb12b",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/store/metadata",
        "type" : "store",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#warehouse/edit?id=5a10ca61-a5a7-11ea-0a80-03a2000bb12b"
      }
    },
    "products" : {},
    "materials" : {}
}

I need to make HTTP Post JSON request but first i need to Get 3 different JSONs and make 1 of them.
I thought better way to do this is to work with Map objects to combine them and then Post.
Now i want to know how to convert Future to Map<String, dynamic>

This is the main.dart to read the json:

import 'dart:convert';
import 'package:api/models/processing.dart';
import 'package:http/http.dart' as http;

main() async {
  fetchProcessing();
}

Future<Processing> fetchProcessing() async {
  final response = await http.get(
      Uri.parse(
          'url'),
      headers: {
        'authorization': 'Bearer token',
        'Content-Type': 'application/json',
        "Access-Control_Allow_Origin": "*",
      });
  if (response.statusCode == 200) { 

    return Processing.fromJson(response.body);
    
  } else {
    print(response.statusCode);
    throw Exception('Failed to load JSON');
  }
}

Here is Model class for JSON:

import 'dart:convert';

class Processing {
  Processing({
    required this.organization,
    required this.processingSum,
    required this.quantity,
    required this.processingPlan,
    required this.productsStore,
    required this.materialsStore,
    required this.products,
    required this.materials,
  });

  final MaterialsStore organization;
  final double processingSum;
  final double quantity;
  final MaterialsStore processingPlan;
  final MaterialsStore productsStore;
  final MaterialsStore materialsStore;
  final Materials products;
  final Materials materials; 

  factory Processing.fromJson(Map<String, dynamic> json) => Processing(
        organization: MaterialsStore.fromJson(json["organization"]),
        processingSum: json["processingSum"],
        quantity: json["quantity"],
        processingPlan: MaterialsStore.fromJson(json["processingPlan"]),
        productsStore: MaterialsStore.fromJson(json["productsStore"]),
        materialsStore: MaterialsStore.fromJson(json["materialsStore"]),
        products: Materials.fromJson(json["products"]),
        materials: Materials.fromJson(json["materials"]),
      );

  Map<String, dynamic> toJson() => {
        "organization": organization.toJson(),
        "processingSum": processingSum,
        "quantity": quantity,
        "processingPlan": processingPlan.toJson(),
        "productsStore": productsStore.toJson(),
        "materialsStore": materialsStore.toJson(),
        "products": products.toJson(),
        "materials": materials.toJson(),
      };
}

class Materials {
  Materials(); 

  factory Materials.fromJson(Map<String, dynamic> json) => Materials();

  Map<String, dynamic> toJson() => {};
}

class MaterialsStore {
  MaterialsStore({
    required this.meta,
  });

  final Meta meta;  

  factory MaterialsStore.fromJson(Map<String, dynamic> json) => MaterialsStore(
        meta: Meta.fromJson(json["meta"]),
      );

  Map<String, dynamic> toJson() => {
        "meta": meta.toJson(),
      };
}

class Meta {
  Meta({
    required this.href,
    required this.metadataHref,
    required this.type,
    required this.mediaType,
    required this.uuidHref,
  });

  final String href;
  final String metadataHref;
  final String type;
  final String mediaType;
  final String uuidHref;  

  factory Meta.fromJson(Map<String, dynamic> json) => Meta(
        href: json["href"],
        metadataHref: json["metadataHref"],
        type: json["type"],
        mediaType: json["mediaType"],
        uuidHref: json["uuidHref"],
      );

  Map<String, dynamic> toJson() => {
        "href": href,
        "metadataHref": metadataHref,
        "type": type,
        "mediaType": mediaType,
        "uuidHref": uuidHref,
      };
}

Here is JSON i'm parsing:

{   
    "organization" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/organization/5a0f5b7b-a5a7-11ea-0a80-03a2000bb129",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/organization/metadata",
        "type" : "organization",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#mycompany/edit?id=5a0f5b7b-a5a7-11ea-0a80-03a2000bb129"
      }
    },   
    "processingSum" : 10000.0,
    "quantity" : 3.0,
    "processingPlan" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/processingplan/2b0530e5-57e2-11ec-0a80-027e001adc60",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/processingplan/metadata",
        "type" : "processingplan",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#processingplan/edit?id=2b0530e5-57e2-11ec-0a80-027e001adc60"
      }
    },
    "productsStore" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/store/5a10ca61-a5a7-11ea-0a80-03a2000bb12b",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/store/metadata",
        "type" : "store",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#warehouse/edit?id=5a10ca61-a5a7-11ea-0a80-03a2000bb12b"
      }
    },
    "materialsStore" : {
      "meta" : {
        "href" : "https://online.moysklad.ru/api/remap/1.2/entity/store/5a10ca61-a5a7-11ea-0a80-03a2000bb12b",
        "metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/store/metadata",
        "type" : "store",
        "mediaType" : "application/json",
        "uuidHref" : "https://online.moysklad.ru/app/#warehouse/edit?id=5a10ca61-a5a7-11ea-0a80-03a2000bb12b"
      }
    },
    "products" : {},
    "materials" : {}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2025-01-17 02:09:18

使用then()whenComplete()。例子:

fetchProcessing().then((value) {
  // your code
});

Use then() or whenComplete(). Example:

fetchProcessing().then((value) {
  // your code
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文