Mindinventory / flutter-retrofit

API calls using the retrofit client.
https://www.mindinventory.com/flutter-app-development.php
MIT License
51 stars 21 forks source link

getPosts() && getAllComments() #4

Closed Gobmichet closed 3 years ago

Gobmichet commented 3 years ago

Hi, I'm testing the retrofit package thanks to your article on medium and the code i dowloaded on github... (thanks bytw) It works well but i then want to manipulate it... Unfortunatly i have some troubles implementing the

@GET(Apis.users)
Future<List<User>> getAllUsers();

and even more ::

_@GET("/posts")
Future<List<Post>> getPosts(@Header("Content-Type") String contentType );
@GET("/comments")
@Headers(<String, dynamic>{ //Static header
  "Content-Type" : "application/json",
  "Custom-Header" : "Your header"
})
Future<List<Comment>> getAllComments();_

WebServices calls... Indeed it just answers me for the

@GET(Apis.users)
 Future<List<User>> getLesUsers()

that :: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: DioError [DioErrorType.other]: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List?'

I know the problem is because of the returned status and meta that you handle in responseData where you await a Future

But since you gave those examples Future<List> // Future<List> // Future<>

Could you please give us the missing code in order to make let's say

@GET(Apis.users)
Future<List<User>> getAllUsers()

work please ?

I was used to the android retrofit lib (I'm a 4 Years XP android dev but only 1 month flutter one :p ) but there are some real differences here and i'm quite confused...

Would really appreciate, Thanks in advance :)

abrarmalekji12 commented 3 years ago

Hii, This error comes because response is type of Map<String,dynamic>, where there are three fields code, meta and data, better way is to use ResponseData , so that you can get error code. but if you want to only use data then, you can create separate class. @GET(Apis.users) Future<UserList> getUsers(); `class UserList { List? _data;

List? get data => _data;

UserList({ List? data}){ // _data = data; }

UserList.fromJson(dynamic json) { if (json["data"] != null) { _data = []; json["data"].forEach((v) { _data?.add(User.fromJson(v)); }); } }

Map<String, dynamic> toJson() { var map = <String, dynamic>{}; if (_data != null) { map["data"] = _data?.map((v) => v.toJson()).toList(); } return map; }

}`

Gobmichet commented 3 years ago

Thanks for your answer but you quite do the same thing indeed. Making a class over entities, that's quite the same as the ResponseData.

I just was wondering if, with the public API used in the example, it was possible to directly get a List without wrapping it in a responseData in order ro handle the code and meta stuff...

cause in "real life", codestatus and meta won't be in the parsed body right ? so IRL you should directly get a List of User (Entity) instead of a responseData that holds in itself a List of User no ?

PS : in case of such an error (response's mismatching type), is there a way to see what the server answer anyway in order to correct the responseData file ?

abrarmalekji12 commented 3 years ago

Hii, @GET(Apis.users) Future<List<User>> getAllUsers(); this was given as demo based example, it does not mean that you can get any response as List, it depends on API response, so you have to check manually response type and create Wrapper class based on your response type, For this example, getUsers Api response is not list (it is an object). so, right now we are using our own created wrapper class which is exactly related to our response. Hope this helps. Thanks!

Gobmichet commented 3 years ago

Oh ok well this was the answer i wanted ^^ 'cause reading the tutorial i really thought it was possible with the github code...

So the answer is just no it is not possible, if there are other things in the answer than the list of data you want then you have to make an ResponseData object that will manage those things AND the very data you want wrapping the whole response including what you need...

Feel so sorry it was just a misunderstanding of mine :s Thanks