微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter 改造 DioError [DioErrorType.other]:期望类型为 'List<dynamic>?' 的值,但得到类型为 'String'

如何解决Flutter 改造 DioError [DioErrorType.other]:期望类型为 'List<dynamic>?' 的值,但得到类型为 'String'

我的响应json如下

[
{
  "cat_id":"517","categoryName":"Pizza","item" : [
    {
     "itemId" : "1","name": "Pizza 1","price": 50
    },{
     "itemId":"2","name" :"Pizza 2","price": 40
    }
  ]
},{
"cat_id":"518","categoryName":"Burger","name": "Burger 1","price": 10
    },"name" :"Burger 2","price": 30
    }
  ]
  }
 ]

我已经使用了像下面这样的改造

@RestApi(baseUrl: "https://raw.githubusercontent.com/")
abstract class RestClient {
  factory RestClient(dio dio) = _RestClient;

  @GET("enamul95/categoryshop/main/lib/util/items.json")
  Future<List<ProductMoel>> getProductItemst();
}

@JsonSerializable()
class ProductMoel {
  String categoryName;
  @JsonKey(name: 'item')
  List<Items> item;
  ProductMoel(this.categoryName,this.item);

  factory ProductMoel.fromJson(Map<String,dynamic> json) =>
      _$ProductMoelFromJson(json);

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

@JsonSerializable()
class Items extends Object {
  String itemId;
  String name;
   int price;

  Items(this.itemId,this.name,this.price);

  factory Items.fromJson(Map<String,dynamic> json) => _$ItemsFromJson(json);

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


    @JsonSerializable()
    class Items extends Object {
      String itemId;
      String name;
      // int price;

      Items(this.itemId,this.name);

      factory Items.fromJson(Map<String,dynamic> json) => _$ItemsFromJson(json);

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

我从这里打来电话

void getResutrantList(BuildContext context) async {    
final client =  RestClient(dio(BaSEOptions(contentType: "application/json")));
client.getProductItemst().then((it) {     
  print(it);      
}).catchError((error,stackTrace) {     
  print("inner  **********************: $error");
});
}

我的代码有什么问题。请帮帮我..

解决方法

由于您正在查询文件,我猜 Github 发送的数据没有 Content-Type: application/json 标头,因此 Dio 将其作为纯文本进行管理。

如果您需要模拟 API,您可以使用在线托管的 MockAPI.io 或本地运行的软件 Mockoon。它应该可以解决您的问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。