below is the complete code, I have added the issue description before each line when the issue occurred:
class AuthService {
//sign up user
void signUpUser({
required BuildContext context,
required String email,
required String password,
required String name,
}) async {
try {
User user = User(
id: '',
name: name,
password: password,
email: email,
address: '',
type: '',
token: '',
// cart: [],
);
http.Response res = await http.post(
Uri.parse('$uri/api/signup'),
body: user.toJson(),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
httpErrorHandle(
response: res,
context: context,
onSuccess: () {
showSnackBar(
context,
'Account created! Login with the same credentials!',
);
},
);
} catch (e) {
showSnackBar(context, e.toString());
}
}
// sign in user
void signInUser({
required BuildContext context,
required String email,
required String password,
}) async {
try {
http.Response res = await http.post(
Uri.parse('$uri/api/signin'),
body: jsonEncode({
'email': email,
'password': password,
}),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
//print(res.body);
httpErrorHandle(
response: res,
context: context,
onSuccess: () async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Do not use BuildContexts across async gaps.dartuse_build_context_synchronously
Provider.of<UserProvider>(context, listen: false).setUser(res.body);
await prefs.setString('x-auth-token', jsonDecode(res.body)['token']);
//Do not use BuildContexts across async gaps.dartuse_build_context_synchronously
Navigator.pushNamedAndRemoveUntil(
context,
HomeScreen.routeName,
(route) => false,
);
},
);
} catch (e) {
showSnackBar(context, e.toString());
}
}
// get user data
void getUserData (
BuildContext context,
) async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? token = prefs.getString('x-auth-token');
if(token == null ){
prefs.setString('x-auth-token', '');
}
var tokenRes = await http.post
(Uri.parse('$uri/tokenIsValid'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'x-auth-token': token!
},
);
var response = jsonDecode(tokenRes.body);
if(response == true){
http.Response userRes = await http.get
(Uri.parse('$uri/'),headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'x-auth-token': token
}, );
//Do not use BuildContexts across async gaps.dartuse_build_context_synchronously
var userProvider = Provider.of<UserProvider>(context, listen: false);
userProvider.setUser(userRes.body);
}
} catch (e) {
showSnackBar(context, e.toString());
}
}
}
I am not using StatefulWidget and StatelessWidget. I have tried the below but didn't reach a solution yet:
I have used if (mounted) and I am getting this error as well The undefined name 'mounted'. Try correcting the name to one that is defined, or defining the name.