I am calling a POST request API that takes the inputs and then returns his/her details. So I have implemented most of it and I am able to retrieve the data from the API and print that response data in the terminal. But I need to display that data in the UI. I have added a check that whether the snapshot has data or not, so every time I perform the to display the data I am unable to do that and also since I am handling the API calls it always prints the something have gone wrong.
I am attaching my code snippets so that they might help you understand where I am going wrong. Also, refer to all the comments I have made wherever I thought I am making mistake.
API calling function:
Future<PanCardVerify> panCardVerify(dynamic param) async {
var client = http.Client();
String? token = await storage.readSecureToken('key');
if (token == null) {
throw Exception("No token stored in storage");
}
try {
var response = await client
.post(
Uri.https("baseURL", "endpoint"),
headers: <String, String>{
'Authorization': 'Token $token',
},
body: param)
.timeout(Duration(seconds: TIME_CONST))
.catchError(handleError);
if (response.statusCode == 200) {
print('Response Body: ${response.body}');
final data = await jsonDecode(response.body);
return PanCardVerify.fromJson(data); //This is where I believe I am supposed to return to the snapshot
);
} else if (response.statusCode == 400) {
print("Invalid PAN Card Details");
return param;
} else {
print("Bad Input");
return param;
}
} on SocketException {
throw FetchDataException('message', 'url');
} on TimeoutException {
throw ApiNotRespondingException("message", "url");
}
}
Model Class:
import 'dart:convert';
PanCardVerify transactionFromJson(String str) =>
PanCardVerify.fromJson(json.decode(str));
String transactionToJson(PanCardVerify data) => json.encode(data.toJson());
class PanCardVerify {
PanCardVerify({
required this.status,
this.panData,
});
String status;
PanData? panData;
factory PanCardVerify.fromJson(Map<String, dynamic> json) => PanCardVerify(
status: json["status"],
panData: PanData.fromJson(json["pan_data"]),
);
Map<String, dynamic> toJson() => {
"status": status,
"pan_data": panData?.toJson(),
};
}
class PanData {
PanData({
required this.code,
required this.timestamp,
required this.transactionId,
required this.data,
});
int code;
int timestamp;
String transactionId;
Data data;
factory PanData.fromJson(Map<String, dynamic> json) => PanData(
code: json["code"],
timestamp: json["timestamp"],
transactionId: json["transaction_id"],
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"code": code,
"timestamp": timestamp,
"transaction_id": transactionId,
"data": data.toJson(),
};
}
class Data {
Data({
required this.entity,
required this.pan,
required this.fullName,
required this.status,
required this.category,
});
String entity;
String pan;
String fullName;
String status;
String category;
factory Data.fromJson(Map<String, dynamic> json) => Data(
entity: json["@entity"],
pan: json["pan"],
fullName: json["full_name"],
status: json["status"],
category: json["category"],
);
Map<String, dynamic> toJson() => {
"@entity": entity,
"pan": pan,
"full_name": fullName,
"status": status,
"category": category,
};
}
Exception Controller Class:
class BaseController {
void handleError(error) {
hideLoading();
if (error is BadRequestException) {
var message = error.message;
DialogHelper.showErroDialog(description: message);
} else if (error is SocketException) {
print(error);
} else {
print("Error in the else block");
}
}
Widget where I am trying to display the fullName:
FutureBuilder<PanCardVerify>(
future: ApiService().panCardVerify(context),
builder: (context, snapshot) {
if (snapshot.hasData) {
PanData? data = snapshot.data!.panData;
return Flexible(
child: Text('${data!.data.fullName}'),
);
} else {
return Text("Something went wrong");
}
}),