Home > OS >  How to parse json list into a string - Tweak the code so that I can display a single Json, just as I
How to parse json list into a string - Tweak the code so that I can display a single Json, just as I

Time:11-28

I am getting this error:

A value of type 'UserModel' can't be assigned to a variable of type 'List<UserModel>'. 

I am using Postman to generate my api service code (http request that get api from internet). And I am using https://app.quicktype.io/ to generate my User Model class.

I saw a flutter tutorial which parsed json into a List because the json was actually a List itself. But In this case, Json is a single item (see pictures for difference). How do i Parse it? and display in a ListView.builder widget?

You can re create this problem using this api link: A single Json List of Json error code

CodePudding user response:

Your api response is a Map. You should use ["data"] on it if you want to access the list inside it, like this:

 List<UserModel> userModelFromJson(String str)-> List<UserModel>.from(((json.decode(str)["data"]) as List).map((x)-> UserModel.fromJson(x)));
  • Related