I am trying to build an application with flutter and laravel api. it was going good till i started implementing the login
functionality. I am getting this error [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The getter 'length' was called on null.
Can you please tell me what is wrong with my code?
In case I made a silly mistake, please don't be mad I am just getting started with programming.
Thanks a lot!
Login function code
var _userService = UserService();
var registeredUser = await _userService.login(user);
var result = json.decode(registeredUser.body);
print(result);
if(result['success']['result']== true){
SharedPreferences _prefs = await SharedPreferences.getInstance();
_prefs.setInt('userId', result['success']['user']['id']);
_prefs.setString('userName', result['success']['user']['name']);
_prefs.setString('userPhone', result['success']['user']['phone']);
_prefs.setString('userEmail', result['success']['user']['email']);
_prefs.setString('token', result['success']['token']);
// Timer(Duration(seconds: 2), () {
// Navigator.pop(context);
// Navigator.popAndPushNamed(context, DashboardScreen.id);
// });
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
} else {
displayToastMessage('Failed to login!', context);
}
}```
When I press `Login button` this is how I call the `login` function.
```onPressed: () {
if(!emailTextEditingController.text
.contains("@")) {
displayToastMessage(
"Not a valid email address", context);
}
else if(passwordTextEditingController.text.length < 6) {
displayToastMessage(
"Password must be at least 6 characters", context);
} else {
var user = User();
// user.name = nameTextEditingController.text;
user.email = emailTextEditingController.text;
// user.phone = phoneTextEditingController.text;
user.password = passwordTextEditingController.text;
_login(context, user);
}
},```
This is the UsersService Code for login
`login(User user) async {
return await _repository.httpPost('login', user.toJson());
}`
and `_repositpry` is the one sending request to the `api`
here is how it is implemented
` String _baseUrl = 'https://api.adikatour.com/api';
httpPost(String api, data) async {
return await http.post(_baseUrl + "/" + api, body: data);
}`
and here is the api code for login
` public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
$success['user'] = $user;
return response()->json(['success' => $success], $this->successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}`
can you please tell me why I am getting this error? thanks again!
Finally, I solved the problem. This is how I structured my used model.
class User {
int id;
String name;
String email;
String phone;
String password;
toJson(){
return {
'id' : id.toString(),
'name' : name,
'email' : email,
'phone' : phone,
'password' : password,
};
}
}
But in the _login function, only email and password were set. since my server doesn't allow null values for phone
and name
fields, I have to convert those fields to not null. so, I converted the user model like this. then it is solved.
class User {
int id;
String name;
String email;
String phone;
String password;
toJson(){
return {
'id' : id.toString(),
'name' : name.toString(),
'email' : email.toString(),
'phone' : phone.toString(),
'password' : password.toString(),
};
}
}
Thank you guys for helping me, I really appreciate it. Special thanks to @sajith lakmal