'flutter: Exception DioError [DioErrorType.DEFAULT]: type 'String' is not a subtype of type 'Map<String, dynamic>'

I am new in flutter and I am not able to solve this issue can anyone help me? I am able to get data in string if I type Future<String> login() instead of Future<WrappedResponse> login() this and it will print on Presenter class which is given below.

This is my api class

import 'dart:io';
import 'dart:math';
import 'package:ceee_app/converters/wrapped_response.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:retrofit/retrofit.dart';

part 'api_service.g.dart';

@RestApi(baseUrl: "https://******.com/_dev/api/v1/")
abstract class RestClient {
  factory RestClient(Dio dio) = _RestClient;

  @FormUrlEncoded()
  @POST("login")
  Future<WrappedResponse> login(@Field("email") String email, @Field("password") String password, @Field("device_token") String token, @Field("device_type") String type);
}

This is my wrapper class

import 'package:ceee_app/model/user.dart';
import 'package:json_annotation/json_annotation.dart';

part 'wrapped_response.g.dart';

@JsonSerializable()

class WrappedResponse{
  @JsonKey(name: "message")
  String message;
  @JsonKey(name: "status")
  String status;
  @JsonKey(name: "result")
  User data;

  WrappedResponse();

  factory WrappedResponse.fromJson(Map<String, dynamic> json) => _$WrappedResponseFromJson(json);
  Map<String, dynamic> toJson() => _$WrappedResponseToJson(this);

}

This is my user class

import 'package:json_annotation/json_annotation.dart';

part "user.g.dart";

@JsonSerializable()
class User{
  @JsonKey()
  int id;
  @JsonKey()
  String name;
  @JsonKey()
  String l_name;
  @JsonKey()
  String email;
  @JsonKey()
  String session_token;
  @JsonKey()
  String device_token;

  User();

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

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

}

This is my Presenter class

import 'package:ceee_app/contracts/login_activity_contract.dart';
import 'package:ceee_app/model/user.dart';
import 'package:ceee_app/webservices/api_service.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LoginActivityPresenter implements LoginActivityInteractor {
  LoginActivityView view;
  LoginActivityPresenter(this.view);
  RestClient api = RestClient(Dio());

  @override
  void success(String token) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString("api_token", token);
  }

  @override
  void destroy() => view = null;

  @override
  void login(String email, String password, String token, String type) async {
    await api.login(email, password, token, type).then((it){
      debugPrint("Data : "+it.toString());

    }).catchError((e){
      print("Exception $e");
      view?.toast("There is an error!");
    });
  }
}

And my response is

{
  "status": 1,
  "message": "Login successful!",
  "result": {
    "id": 20,
    "session_token": "YXBpX3Rva2VuNWY3YzJmODM2ZTgyNjUuNTY1MjUwNzAxNjAxOTc0MTQ3",
    "name": "abd",
    "l_name": "xyz",
    "email": "[email protected]",
    "device_token": "fKrw8mpYT96fWIfaxrF26r:APA91bGZUW1wGSmdNMNb",
  }
}


Solution 1:[1]

You forgot to add the required constructor parameters for JSON serialization.

@JsonSerializable()
class WrappedResponse{
  @JsonKey(name: "message")
  String message;
  @JsonKey(name: "status")
  String status;
  @JsonKey(name: "result")
  User data;

  WrappedResponse({this.message, this.status, this.data});

  factory WrappedResponse.fromJson(Map<String, dynamic> json) => _$WrappedResponseFromJson(json);
  Map<String, dynamic> toJson() => _$WrappedResponseToJson(this);
}

The same applies to the User class.

@JsonSerializable()
class User{
  ...
  User({this.name, this.l_name, this.email, session_token, this.device_token});  
  ...
}

Solution 2:[2]

I got response as string. But i have converted as JSON using following steps:

  final dio = Dio();        
    dio.interceptors.add(JsonResponseConverter());
            
        json_response_convert.dart
        
            import 'package:dio/dio.dart';
            import 'dart:convert';
            
            class JsonResponseConverter extends Interceptor {
              @override
              void onResponse(Response response, ResponseInterceptorHandler handler) {
                response.data = json.decode(response.data);
                super.onResponse(response, handler);
              }
            }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omatt
Solution 2 Android Developer World