I'm sending a post request phone number and password to the database and if user send incorrect credentials then it shows the message "the user is not registered". But in else condition it is showing an error on return message "A value of type 'String' can't be returned from the method 'makeRequestLogin' because it has a return type of 'Future<Login?>'."
Future<Login?> makeRequestLogin(String mobileNumber, String password) async {
var response = await http.post(Uri.parse('$baseURL/customer/login'), body: {
"phone_number": mobileNumber,
"password": password,
"registration_type": "normal"
});
if (response.statusCode == 200) {
final responseString = response.body;
final data = jsonDecode(responseString);
Login signUp = Login.fromJson(data);
return signUp;
} else {
final responseString = response.body;
var result = json.decode(responseString);
String message = result["message"];
return message; // On this line getting an error
}
}
Model
class Login {
String? phoneNumber;
String? password;
Login({
this.phoneNumber,
this.password,
});
Login.fromJson(Map<String, dynamic> json) {
phoneNumber = json["phone_number"]?.toString();
password = json["password"]?.toString();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data["phone_number"] = phoneNumber;
data["password"] = password;
return data;
}
}
CodePudding user response:
Instead of returning Login? return String?.
Convert
Future<Login?> makeRequestLogin(String mobileNumber, String password) async
to
Future<String?> makeRequestLogin(String mobileNumber, String password) async
Change returning Login instances to String.
CodePudding user response:
Your return type of that particular future function is log in as you are writing inside the angle brackets so any return within the body of that function should be of type login and not a simple string.
One solution is to have a string error field in your model class and access that when error occurred
