After Signup through rest api i want to get user id in create profile in order to use it when i upload images, video and other information. here is my signup api
Future signupUser(String name, String email, String password, String type) async {
String url = 'myapi';
try{
final response = await http.post(Uri.parse(url),
body: {
'name': name,
'email': email,
'password': password,
'type': type
}
);
var convertedDataJson = jsonDecode(response.body);
return convertedDataJson;
} catch(e){
print(e);
}
here is i am navigating next screen after post sign up api
onPressed: () async {
if (_formKey.currentState!.validate()) {
var nameController = name.text;
var emailController = email.text;
var passwordController = password.text;
var userType = _user;
var rsp = await signupUser(
nameController,
emailController,
passwordController,
userType.toString(),
);
print(rsp);
if (rsp['status'] == true) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
OtpScreen(emailValue: email.text,)),
);
} else {
print('signup failed');
}
}
after sign up it create user id and i want to get that user id profile page how i can get it
here is create profile api
Future<ProfileModel> createProfile(
String userId,
String name,
String profileImage,
String gender,
String age,
String height,
String position,
String country,
String countryFlag,
String clubName,
String clubLogo,
String bio,
) async {
String url = 'http://bitborgtech.com/appss/api/update_profile';
var response = await http.post(Uri.parse(url),
body: {
'userId': userId,
'name': name,
'profileImage' : profileImage,
'gender': gender,
'age' : age,
'height': height,
'position': position,
'country': country,
'countryFlag': countryFlag,
'clubName': clubName,
'clubLogo': clubLogo,
'bio': bio
}
);
var createData = jsonDecode(response.body);
return createData;
}
i want tio get user id after sign up to use it in profile creation and image uploading kindly help
CodePudding user response:
you can save user id in shared preferences (https://pub.dev/packages/shared_preferences) and get when ever you want
Save id
Future<bool> setIsLogin() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('isLogin', true);
prefs.setString('token', _systemUserModel.token);
prefs.setString('id', _systemUserModel.id);
prefs.setString('status', _systemUserModel.status);
prefs.setString('email', _systemUserModel.email);
prefs.setString('name', _systemUserModel.name);
prefs.setString('tenantId', _systemUserModel.tenantId);
return true;
}
Read id
Future<bool> fetchIsLogin() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isLogin = false;
if (prefs.containsKey('isLogin')) {
if (prefs.getBool('isLogin')!) {
String? token = prefs.getString('token');
String? id = prefs.getString('id');
String? status = prefs.getString('status');
String? email = prefs.getString('email');
String? name = prefs.getString('email');
String? tenantId = prefs.getString('tenantId');
_systemUserModel =
SystemUserModel(token!, id!, status!, email!, name!, tenantId!);
isLogin = true;
} else {
isLogin = false;
}
} else {
isLogin = false;
}
notifyListeners();
return isLogin;
}
you can write save function in signup/logon class and read function what ever you need read id
But best practises is set this _systemUserModel
object in ChangeNotifierProvider
and access anywhere you want using provider package (https://pub.dev/packages/provider)