flutter 更改服务器后不显示来自API的数据,未处理的异常:类型“String”不是类型“int”的子类型[已关闭]

5sxhfpxr  于 2023-02-09  发布在  Flutter
关注(0)|答案(2)|浏览(115)
    • 已关闭**。此问题需要超过focused。当前不接受答案。
    • 想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

昨天关门了。
Improve this question
我们有一个网站(PHP-Laravel)和应用程序在谷歌播放和应用程序商店(Flutter).
我们需要改变服务器平面,托管的支持团队从新托管转移所有cPanel配置和数据,转移后网站运行良好,但商店中的应用程序(Flutter)不显示API的数据。
我需要从服务器而不是从应用程序代码的解决方案,因为它已经在线

flutter: Refresh here
flutter: [conection]: true
flutter: 200
flutter: {"status":true,"message":"Success","data":{"slider":[{"id":27,"image":"sliders\/1657913499slider-img.png","created_at":"2022-07-15 15:31:39","updated_at":"2022-07-15 15:31:39","title":null,"body":null,"url":null},{"id":28,"image":"sliders\/1658060417slider-img.png","created_at":"2022-07-17 08:20:18","updated_at":"2022-07-17 08:20:18","title":null,"body":null,"url":null},{"id":22,"image":"sliders\/1654451985slider-img.png","created_at":"2022-06-05 13:59:45","updated_at":"2022-06-05 14:00:22","title":null,"body":null,"url":null}],"categories":[{"id":2,"name":"\u0633\u064a\u0627\u0631\u0627\u062a","image":"categories\/HmbZuA25Nu5FwR4YscS80Zy2UooipznXFBgXCzvv.jpeg","order":"1","parent_id":null,"created_at":"2019-12-30 03:59:51","updated_at":"2020-07-23 02:55:21"},{"id":19,"name":"\u0639\u0642\u0627\u0631\u0627\u062a","image":"categories\/1583329844category-img.jpeg","order":"2","parent_id":null,"created_at":"2020-03-04 05:50:44","updated_at":"2020-07-20 00:50:50"},{"id":22,"name":"\u0645\u<…>
flutter: object1
flutter: {slider: [{id: 27, image: sliders/1657913499slider-img.png, created_at: 2022-07-15 15:31:39, updated_at: 2022-07-15 15:31:39, title: null, body: null, url: null}, {id: 28, image: sliders/1658060417slider-img.png, created_at: 2022-07-17 08:20:18, updated_at: 2022-07-17 08:20:18, title: null, body: null, url: null}, {id: 22, image: sliders/1654451985slider-img.png, created_at: 2022-06-05 13:59:45, updated_at: 2022-06-05 14:00:22, title: null, body: null, url: null}], categories: [{id: 2, name: سيارات, image: categories/HmbZuA25Nu5FwR4YscS80Zy2UooipznXFBgXCzvv.jpeg, order: 1, parent_id: null, created_at: 2019-12-30 03:59:51, updated_at: 2020-07-23 02:55:21}, {id: 19, name: عقارات, image: categories/1583329844category-img.jpeg, order: 2, parent_id: null, created_at: 2020-03-04 05:50:44, updated_at: 2020-07-20 00:50:50}, {id: 22, name: مجوهرات, image: categories/1583332248category-img.jpeg, order: 3, parent_id: null, created_at: 2020-03-04 06:30:48, updated_at: 2020-07-26<…>
flutter: slider
flutter: categories
[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: type 'String' is not a subtype of type 'int'
#0      new Category.fromJson (package:haraj_plus_app/models/api/category.dart:14:5)
#1      new NewHomeModel.fromJson.<anonymous closure> (package:haraj_plus_app/models/api/getHome.dart:33:33)
#2      List.forEach (dart:core-patch/growable_array.dart:416:8)
#3      new NewHomeModel.fromJson (package:haraj_plus_app/models/api/getHome.dart:32:26)
#4      new BaseGenericObjectResponse.fromJson (package:haraj_plus_app/models/base_generic_object_response.dart:43:31)
#5      ApiBuilder.performObjectRequest (package:haraj_plus_app/api/api_builder.dart:90:11)
<asynchronous suspension>
class Category {
  late int id;
  late String name;
  late String image;
  late int order;
  late int? parentId;
  late String createdAt
  late String updatedAt;

  Category.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    image = json['image'];
    order = json['order'];
    parentId = json['parent_id'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['image'] = this.image;
    data['order'] = this.order;
    data['parent_id'] = this.parentId;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

php版本在7.3,我试图改变它来解决这个问题,但我失败了。acaully我不能idintfiy这个问题

ql3eal8s

ql3eal8s1#

“String”不是类型“int”的子类型。将String赋给int类型时,会发生此错误。
检查intid,intorder,intparentId,看看你得到了什么值,然后改变它们的数据类型

798qvoo8

798qvoo82#

根据您共享的JSON,您有"order":"2",因此,您尝试将"2"存储在int上。
与PHP代码(您尚未共享)相关,假设您的代码如下所示:

public function whatever(Request $request)
{
    $model = Model::first();

    return [
        // Other fields
        'order' => (int) $model->order,
    ];
}

您必须在设置order的位置之前添加(int),以便将其转换为integer。但是没有代码,我只是展示它应该是什么样子...

相关问题