I am trying to perform user login with my flutter app but then I keep getting data != null error and that a Text Widget must have a non-null string.
Upon further debugging, I realized the response['message'] is printing a null value so I implement a condition to check if it's not null before proceeding but yet still it keeps giving me the same error.
When I use response['message'].toString(), it still gives the same error.
this is the full error being thrown 'data != null': A non-null String must be provided to a Text widget.
the issue seems to be from the response['message'] but I just can't seem to find ways to solve it
This is Auth controller class
class AuthController extends GetxController {
AuthService authService = AuthService();
ProjectApis projectApis = ProjectApis();
String name = '';
String email = '';
String password = '';
String confirmPassword = '';
var isPasswordHidden = true.obs;
Future loginUser(BuildContext context) async {
buildLoader(context, message: 'Loading...');
http.Response response = await authService.signInUser(
email,
password,
);
if (response.statusCode == 200) {
Map<String, dynamic> responseData = json.decode(response.body);
debugPrint(responseData.toString());
debugPrint(responseData['message']);
if (responseData["status"] == true) {
User user = User.fromJson(responseData);
UserPreferences().setUser(user);
Navigator.pop(context);
Get.offAll(() => BottomNavigation());
return;
} else {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(responseData['message']),
));
return;
}
} else {
Navigator.pop(context);
showErrorDialog(context, message: "Server Error");
return;
}
}
}
This is the sign in function
Future<http.Response> signInUser(
String email,
String password,
) async {
Map data = {
'email': email,
'password': password,
};
var body = json.encode(data);
var url = Uri.parse(projectApis.loginUrl);
var response = await client.post(
url,
body: body,
headers: projectApis.headers,
);
return response;
}
This is the User model class
User userFromJson(String str) => User.fromJson(json.decode(str));
String userToJson(User data) => json.encode(data.toJson());
class User {
User({
this.id,
this.name,
this.email,
this.password,
this.passwordConfirm,
this.token,
});
int? id;
String? name;
String? email;
String? password;
String? passwordConfirm;
String? token;
String applicationDirPath = "";
factory User.fromJson(Map<String, dynamic> json) => User(
id: json["id"],
name: json["name"],
email: json["email"],
password: json["password"],
passwordConfirm: json["passwordConfirm"],
token: json["token"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"email": email,
"password": password,
"passwordConfirm": passwordConfirm,
"token": token,
};
}
CodePudding user response:
Use null-operator like here
response['message'] ?? ''
If left side was null the right side will assing But you can use this just if you are sure this happen because of this line
CodePudding user response:
I think problem is with the Text(responseData['message']),
line.
Dart can't be shure that me message
key exist on responseData Map. So Text(responseData['message']),
can be null which is bad for null safety.
Just do:
String message = responseData['message'] ?? '';
The ??
operator will return an empty string in case ResponseData['message']
is null.
Then replace in your Text widget:
Text(message),
CodePudding user response:
Text widget doesn't accept nullable String, and reading map can provide null value. You can provide default value on null case like
Text(myMap["key"]??"defaultValue")
And for perser you can do
if (responseData["status"] != null && responseData["status"]==true ) {