I am trying to register Users but this throws A non-null String that must be provided to a Text widget error. I have gone through the code to check if I have given a null value to any Text Widget but I couldn't find one as all Text widgets are null sound.
Below is an excerpt of the code
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: ProjectColors.primary,
minimumSize: Size.fromHeight(height * 0.08),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: Text(
'Register',
style: TextStyle(fontSize: 16),
),
onPressed: () async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
await authController.createUserAccount(context);
}
},
),
*when I start debugging, the error seems to be pointed at * await authController.createUserAccount(context); ** and this is confusing me the more.
This is the createUserAccount function
AuthService authService = AuthService();
ProjectApis projectApis = ProjectApis();
String name = '';
String email = '';
String password = '';
String confirmPassword = '';
var isPasswordHidden = true.obs;
Future createUserAccount(BuildContext context) async {
//show loader here
http.Response response = await authService.signUpUser(
name: name,
email: email,
password: password,
confirmPassword: confirmPassword,
);
if (response.statusCode == 200 && response. Body != "") {
Map<String, dynamic> res = json.decode(response. Body);
if (res["status"] == true) {
var user = User.fromJson(res["data"]);
UserPreferences().setUser(user);
Get.offAll(() => GoHome());
return;
} else {
print(res['message']);
return;
}
} else {
Text("Error Occurred");
return;
}
}
And this is the signUpUser function
Future<http.Response> signUpUser({
String? name,
String? email,
String? password,
String? confirmPassword,
}) async {
Map data = {
'name': name,
'email': email,
'password': password,
'password_confirmation': confirmPassword,
};
var body = json.encode(data);
var url = Uri.parse(projectApis.registerUrl);
var response = await http.post(
url,
body: body,
headers: projectApis.headers,
);
return response;
}
I have read through this link1 and this link2
but none of them solve my problem.
Solution 1: Kaushik Chandru
In the createUserAccount method you returned a Text widget. This is creating an error i think
Text("Error Occurred");
Replace this with a print statement for testing
Print("Error Occurred");