Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 无法解码服务器对json颤振的响应_Flutter_Flutter Getx - Fatal编程技术网

Flutter 无法解码服务器对json颤振的响应

Flutter 无法解码服务器对json颤振的响应,flutter,flutter-getx,Flutter,Flutter Getx,我正在尝试使用GetConnect将嵌套对象发送到服务器。然而,在多次尝试之后,我都失败了。我看着弗利特博士;尽管如此,它使用与我相同的JSON.decode。我为后端设置的内容类型是application/json。 我怎样才能解决这个问题 Future<List<MainItem>> addItem() async { var mainItem = MainItem( name: name, description: descripti

我正在尝试使用GetConnect将嵌套对象发送到服务器。然而,在多次尝试之后,我都失败了。我看着弗利特博士;尽管如此,它使用与我相同的JSON.decode。我为后端设置的内容类型是application/json。 我怎样才能解决这个问题

Future<List<MainItem>> addItem() async {
    var mainItem = MainItem(
      name: name,
      description: description,
      itemTypeId: itemTypeId,
      userId: userId,
      itemUuid: itemUuid,
      inventory: Inventory(
          inventoryUuid: '',
          price: 0,
          quantity: 0,
          box: 0,
          pricePerItem: 0,
          itemInBox: 0,
          inStoragePrice: 0,
          inStorageItem: 0),
      image: ItemImage(imageUrl: imageUrl),
      location: InventoryLocation(
          description: locationDescription,
          name: locationName,
          address: Address(
              countryId: countryId,
              stateId: stateId,
              streetAddress: streetAddress,
              city: city,
              zipCode: zipCode)),
    ).toJson();
    print('Values pass to Future: $mainItem', contentType: 'application/json; charset=UTF-8');
    final res = await post('/item/$userId', mainItem);
    if (res.status.hasError) {
      return Future.error(res.statusText!);
    } else {
//I think this shouldn't be decoded since content type is application/json
      return (res.body as List).map((e) => MainItem.fromJson(e)).toList();
    }
  }
如果我运行Chrome进行调试,而不进行热重新加载,则会显示:

 Expected a value of type 'List<MainItem>?', but got one of type 'TypeErrorImpl'
 at Object.throw_ [as throw] (http://localhost:60831/dart_sdk.js:5037:11)
    at Object.castError (http://localhost:60831/dart_sdk.js:5010:15)
    at Object.cast [as as] (http://localhost:60831/dart_sdk.js:5319:17)
    at dart.NullableType.new.as (http://localhost:60831/dart_sdk.js:6826:60)
    at http://localhost:60831/packages/booking_management_dashboard/screen/pages/tablet/widgets/choice_chip/choice_chip.dart.lib.js:6436:49
    at _RootZone.runUnary (http://localhost:60831/dart_sdk.js:37245:59)
    at _FutureListener.then.handleError (http://localhost:60831/dart_sdk.js:32513:48)
    at handleError (http://localhost:60831/dart_sdk.js:33044:51)
    at Function._propagateToListeners (http://localhost:60831/dart_sdk.js:33070:17)
    at _Future.new.[_completeWithValue] (http://localhost:60831/dart_sdk.js:32914:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:60831/dart_sdk.js:32935:35)
    at Object._microtaskLoop (http://localhost:60831/dart_sdk.js:37497:13)
    at _startMicrotaskLoop (http://localhost:60831/dart_sdk.js:37503:13)
    at http://localhost:60831/dart_sdk.js:33274:9
Console.log从后端打印数据:

{
  name: 'aaaa',
  uuid: '34360ad5-0427-410e-bacc-3052bb310cd7',
  item_type_id: 302,
  description: 'sssss',
  sku: null,
  user_id: '82670000-effa-4669-936a-d6c1b1ab0583',
  item_image: {
    image_url: 'https://media.istockphoto.com/vectors/red-rubber-stamp-icon-on-transparent-background-vector-id918650450?k=6&m=918650450&s=612x612&w=0&h=eIpOHzRxfCcSsnV83xddS9Jguvpshj5vcvkOrkmGJXk='
  },
  inventory: {
    price: 66666,
    quantity: 231,
    box: 123,
    itemInBox: 23,
    inStoragePrice: 0,
    pricePerItem: 0,
    inStorageItem: 0
  },
  location: {
    location_name: 'eeee',
    location_description: 'rrrr',
    address: {
      id: 108,
      street_address: 'aaaaa',
      state_id: 7,
      country_id: 1,
      zipcode: 123,
      city: 'sssss'
    }
  }
}

您能把收到的错误信息和模型定义包括在内吗?特别是toJson和fromJson方法。@S.M.JAHANGIR您好,我已经更新了上面的问题。我期待着看到你的答复。谢谢:)你能把回复也包括进来吗。bodyString?@S.M.JAHANGIR嗨,我可能会误解我的问题。我为后端设置的内容类型是application/json。因此,这里的编码步骤可能是不必要的。但是,错误仍然是一样的。我再次更新了问题。谢谢。我希望您打印并包含
res.bodyString
的结果,我指的是您从服务器得到的结果。
class MainItem {
  String? name, description, userId, itemUuid, createdAt;
  int? id, itemTypeId;
  InventoryLocation? location;
  ItemImage? image;
  Inventory? inventory;

  MainItem({
    @required this.name,
    @required this.description,
    @required this.location,
    this.itemUuid,
    this.createdAt,
    this.userId,
    this.id,
    this.itemTypeId,
    this.image,
    this.inventory,
  });

  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    data['name'] = name;
    data['id'] = id;
    data['uuid'] = itemUuid;
    data['user_id'] = userId;
    data['createdAt'] = createdAt;
    data['description'] = description;
    data['item_type_id'] = itemTypeId;
    data['location'] = location!.toJson();
    data['item_image'] = image!.toJson();
    data['inventory'] = inventory!.toJson();

    return data;
  }

  factory MainItem.fromJson(Map<String, dynamic> json) => MainItem(
        id: json['id'] as int? ?? 0,
        itemTypeId: json['item_type_id'] as int? ?? 0,
        name: json['name'] ?? 'Nothing found',
        itemUuid: json['uuid'] ?? 'Nothing found',
        userId: json['user_id'] ?? 'Nothing found',
        createdAt: json['createdAt'] ?? 'Nothing found',
        description: json['description'] ?? 'Nothing found',
        image: ItemImage.fromJson(json['item_image']),
        location: InventoryLocation.fromJson(json['location']),
        inventory: Inventory.fromJson(json['inventory']),
      );

  factory MainItem.fromRawJson(String str) =>
      MainItem.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  @override
  String toString() {
    return 'Values are name: $name, description: $description, location : $location, userId: $userId,  image: $image';
  }
}
Values pass to Future: {name: aaas, id: null, uuid: 295efed7-b8ab-4329-b857-3acb147a2e3c, user_id: 82670000-effa-4669-936a-d6c1b1ab0583, createdAt: null, description: ssss, item_type_id: 302, location: {id: null, location_name: eee, location_description: rrr,
location_uuid: null, address_uuid: null, address: {id: null, street_address: aaaa, state_id: 7, address_uuid: null, zipcode: 123, city: sssss, country_id: 1}}, item_image: {id: null, image_url:
https://media.istockphoto.com/vectors/red-rubber-stamp-icon-on-transparent-background-vector-id918650450?k=6&m=918650450&s=612x612&w=0&h=eIpOHzRxfCcSsnV83xddS9Jguvpshj5vcvkOrkmGJXk=, image_uuid: null}, inventory: {id: null, inventory_uuid: , box: 0, quantity: 0,
price: 0, itemInBox: 0, pricePerItem: 0, inStorageItem: 0, inStoragePrice: 0}}
{
  name: 'aaaa',
  uuid: '34360ad5-0427-410e-bacc-3052bb310cd7',
  item_type_id: 302,
  description: 'sssss',
  sku: null,
  user_id: '82670000-effa-4669-936a-d6c1b1ab0583',
  item_image: {
    image_url: 'https://media.istockphoto.com/vectors/red-rubber-stamp-icon-on-transparent-background-vector-id918650450?k=6&m=918650450&s=612x612&w=0&h=eIpOHzRxfCcSsnV83xddS9Jguvpshj5vcvkOrkmGJXk='
  },
  inventory: {
    price: 66666,
    quantity: 231,
    box: 123,
    itemInBox: 23,
    inStoragePrice: 0,
    pricePerItem: 0,
    inStorageItem: 0
  },
  location: {
    location_name: 'eeee',
    location_description: 'rrrr',
    address: {
      id: 108,
      street_address: 'aaaaa',
      state_id: 7,
      country_id: 1,
      zipcode: 123,
      city: 'sssss'
    }
  }
}