I have cubits file and have an async function that get data from API:
The response data from api looks like this:
{"success":true,"userId":782,"accessToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzgyLCJlbWFpbCI6Im5qZGpmam5AZ21haWwuY29tIiwicHJvZHVjdElkIjoxMCwiZXhwIjoxNjY5NDgwMDM5fQ.6anGdvruukEmxj5GZE1ZCGHYdIrEK5lxOPuMNWGfj-g","refreshToken":"dd4f0b69-3607-4b73-9c04-4fc1a9bd52e0"}
And when I press Button:
LoginButton(
onPressed: () async {
await BlocProvider.of<AuthCubit>(context).signUp();
}
)
And I get 200 status code from api, I get and error => type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response'
This is the function inside cubits file:
Future signUp() async {
try {
final Response response = await _apiService.signUp(
email: state.email ?? "",
password: state.password ?? "",
firstName: state.firstName ?? "",
lastName: state.lastName ?? "",
genderUuid: state.genderUuid ?? "",
ageGroupUuid: state.ageGroupUuid ?? "",
countryUuid: state.countryUuid ?? ""
);
Map<String, dynamic > data = Map<String, dynamic>.from(jsonDecode(response.data));
if (response.statusCode == 200) {
log("I am here");
}
} on DioError catch (ex) {
log("cubit === ${ex.response!.data.toString()}");
emit(ErrorAuthentification(ex.response!.data["statusCode"], ex.response!.data["message"]));
} catch (e) {
log(e.toString()); // type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response<dynamic>'
rethrow; // type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response<dynamic>'
}
}
This is my ApiService:
class ApiService {
final ApiProvider apiProvider = ApiProvider();
Future<dynamic> signUp({
required String email,
required String password,
required String firstName,
required String lastName,
required String genderUuid,
required String ageGroupUuid,
required String countryUuid
}) async {
final data = await apiProvider.signUp(
email: email,
password: password,
firstName: firstName,
lastName: lastName,
genderUuid: genderUuid,
ageGroupUuid: ageGroupUuid,
countryUuid: countryUuid
);
return data;
}
}
And my ApiProvide:
class ApiProvider {
final dio = Dio(BaseOptions(baseUrl: ApiPath.testUrl, contentType: "application/json"));
static final _instance = ApiProvider._internal();
factory ApiProvider() {
return _instance;
}
ApiProvider._internal();
Future<dynamic> _request(Function request) async {
try {
final res = (await request());
//log('res === ${res.statusCode}, ${res.data.runtimeType}, ${res.data}');
return res.data;
} on DioError catch (ex) {
log('Error dio _request === ${ex.response!.statusCode}');
rethrow;
}
}
Future<dynamic> signUp({
required String email,
required String password,
required String firstName,
required String lastName,
required String genderUuid,
required String ageGroupUuid,
required String countryUuid
}) async {
var params = {
"email": email,
"password": password,
"firstName": firstName,
"lastName": lastName,
"genderUuid": genderUuid,
"ageGroupUuid": ageGroupUuid,
"countryUuid": countryUuid,
"deviceId": "fingerprint"
};
dio.interceptors.add(
LogInterceptor(requestBody: true, responseBody: true),
);
try {
return _request(
() => dio.post(
ApiPath.signUp,
data: jsonEncode(params),
options: Options(
responseType: ResponseType.json
)
)
);
} on DioError catch (ex) {
log('Error signIn ==== ${ex.response!.statusCode}');
rethrow;
}
}
}
I don't understand why this error comes even if the request is successful(200 status code)
CodePudding user response:
You already get the data from response in _request
, so you don't need use it again, so change this:
Map<String, dynamic > data = Map<String, dynamic>.from(jsonDecode(response.data));
to this:
Map<String, dynamic > data = Map<String, dynamic>.from(jsonDecode(response));