In the following image, I am using the post method to send the data, which is successfully done and when the server returns the response which is can be seen in the user's console, now how do I display those responses in text widgets in another class?
class APIService {
Future<DoctorResponseLoginModels> register(
DoctorRequestLoginModels doctorLoginRequestModels) async {
String url = "http://202.51.75.142:9028/api/PatientMaster/PostPatientLogin";
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var globalToken = sharedPreferences.getString("token");
print("$globalToken");
http.Response response = await http.post(Uri.parse(url),
headers: {
"Content-Type": "application/json",
'Accept': 'application/json',
'Authorization': 'Bearer $globalToken',
},
body: jsonEncode(doctorLoginRequestModels));
var responseJson = json.decode(response.body.toString());
DoctorResponseLoginModels responseModel =
DoctorResponseLoginModels.fromJson(responseJson);
print("This is ${response.body}");
print("This is noy $responseModel");
String name = responseModel.doctorName.toString();
print(name);
if (response.statusCode == 200) {
sharedPreferences.setInt('code', response.statusCode);
var StatusCode = sharedPreferences.getInt('code');
print("This contains : $StatusCode");
sharedPreferences.setString("data", response.body);
var Dataaa = sharedPreferences.getString("data");
print("$Dataaa");
return DoctorResponseLoginModels.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to create album.');
}
}
}
This is Doctor Response Models Class, now I need this datas from Doctor Response to be displayed in another class in text widget
DoctorResponseLoginModels doctorResponseLoginModelsFromJson(String str) =>
DoctorResponseLoginModels.fromJson(json.decode(str));
String doctorResponseLoginModelsToJson(DoctorResponseLoginModels data) =>
json.encode(data.toJson());
class DoctorResponseLoginModels {
DoctorResponseLoginModels({
this.doctorId,
this.nmCno,
this.doctorName,
this.contactNo,
this.username,
this.emailId,
this.strEmail,
this.id,
this.intMobile,
this.gender,
this.currentAddress,
this.depId,
this.entryDate,
this.password,
this.code,
this.isActive,
this.hospitalName,
this.department,
this.deviceId,
this.profile,
this.token,
this.role,
});
int? doctorId;
String? nmCno;
String? doctorName;
String? contactNo;
dynamic username;
String? emailId;
String? strEmail;
int? id;
String? intMobile;
dynamic? gender;
String? currentAddress;
int? depId;
String? entryDate;
dynamic? password;
dynamic? code;
bool? isActive;
dynamic? hospitalName;
dynamic? department;
dynamic? deviceId;
String? profile;
String? token;
String? role;
factory DoctorResponseLoginModels.fromJson(Map<String, dynamic> json) =>
DoctorResponseLoginModels(
doctorId: json["doctorID"],
nmCno: json["nmCno"],
doctorName: json["doctorName"],
contactNo: json["contactNo"],
username: json["username"],
emailId: json["emailID"],
strEmail: json["strEmail"],
id: json["id"],
intMobile: json["intMobile"],
gender: json["gender"],
currentAddress: json["currentAddress"],
depId: json["depId"],
entryDate: json["entryDate"],
password: json["password"],
code: json["code"],
isActive: json["isActive"],
hospitalName: json["hospitalName"],
department: json["department"],
deviceId: json["deviceId"],
profile: json["profile"],
token: json["token"],
role: json["role"],
);
Map<String, dynamic> toJson() => {
"doctorID": doctorId,
"nmCno": nmCno,
"doctorName": doctorName,
"contactNo": contactNo,
"username": username,
"emailID": emailId,
"strEmail": strEmail,
"id": id,
"intMobile": intMobile,
"gender": gender,
"currentAddress": currentAddress,
"depId": depId,
"entryDate": entryDate,
"password": password,
"code": code,
"isActive": isActive,
"hospitalName": hospitalName,
"department": department,
"deviceId": deviceId,
"profile": profile,
"token": token,
"role": role,
};
}
CodePudding user response:
The correct approach would be to put the API-Class to a Provider.
Access the Instance whenever you need.
CodePudding user response:
You can use FutureBuilder
like this:
FutureBuilder<DoctorResponseLoginModels>(
future: APIService().register(...),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading....');
default:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
DoctorResponseLoginModels data = snapshot.data!;
return Column(
children: [
Text(data.doctorName ?? ''),
Text(data.username?? ''),
],
);
}
}
},
),