jonataslaw / get_cli

Official Getx CLI
Apache License 2.0
590 stars 171 forks source link

Unhandled Exception: type '(dynamic) => Object?' is not a subtype of type '((dynamic) => Todo)?' in type cast #196

Open smjxpro opened 2 years ago

smjxpro commented 2 years ago

I have generated model and provider by: get generate model from 'https://jsonplaceholder.typicode.com/todos/1'

But when doing any request, it throws exception: Unhandled Exception: type '(dynamic) => Object?' is not a subtype of type '((dynamic) => Todo)?' in type cast

The generated provider code:

class TodoProvider extends GetConnect {
  @override
  void onInit() {
    httpClient.defaultDecoder = (map) {
      if (map is Map<String, dynamic>) return Todo.fromJson(map);
      if (map is List) return map.map((item) => Todo.fromJson(item)).toList();
    };
    httpClient.baseUrl = 'https://jsonplaceholder.typicode.com/';
  }

  Future<Todo?> getTodo(int id) async {
    final response = await get('$id');
    return response.body;
  }

  Future<Response<Todo>> postTodo(Todo todo) async => await post('', todo);
  Future<Response> deleteTodo(int id) async => await delete('$id');
}

If I change the httpClient.defaultDecoder to: httpClient.defaultDecoder = (map) { return Todo.fromJson(map); }; works OK. But if I add a method to get the todo list, then does not because as you can see the list decoder is needed but is not provided anymore and throws: Unhandled Exception: type '(dynamic) => Todo' is not a subtype of type '((dynamic) => List<Todo>)?' in type cast

liangsqrt commented 1 year ago

i also meet the same problem

liangsqrt commented 1 year ago

i slove it by this: await post<LoginPhone>('', loginphone, decoder: ((dynamic)=>LoginPhone()));

and it works will