Enamul Haque
Enamul Haque

Reputation: 5063

Flutter retrofit DioError [DioErrorType.other]: Expected a value of type 'List<dynamic>?', but got one of type 'String'

My response json is bellow

[
{
  "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",
  "item" : [
    {
     "itemId" : "1",
     "name": "Burger 1",
     "price": 10
    },
    {
     "itemId":"2",
     "name" :"Burger 2",
     "price": 30
    }
  ]
  }
 ]

I have use retrofit like bellow

@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);
    }

I called from here like bellow

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");
});
}

What is the problem of my code.Please help me..

Upvotes: 0

Views: 1388

Answers (1)

FDuhen
FDuhen

Reputation: 4826

Since you're querying a file, I guess that Github is sending the datas without the Content-Type: application/json header, and for this reason Dio is managing it as a Plain Text.

If you need to mock an API, you could use MockAPI.io which is hosted online, or Mockoon which is a software running locally. It should solve your problem.

Upvotes: 1

Related Questions