'How do i link state provider with my custom signin function to retrive get username
I was trying to retrieve user details from signin function so that I can use it on other screens in the flutter app but it seems it does not work as expected using Provider as state manager.
void LoginFunction() {
if (username.text.isEmpty) {
scaffoldMessenger.showSnackBar(
mySnackBar("Provide username")
);
} else if (password.text.isEmpty) {
scaffoldMessenger.showSnackBar(
mySnackBar("Provide Password")
);
} else {
print(username.text);
print(password.text);
Provider.of<SignInDetailsModel>(context, listen: false).signIn(username.text, password.text);
}}
but if I do not use the . combination in the provider then the signup works but the provider does not work. Any ideas on how to solve it the PHP webserver scripts work perfectly. If anyone has any ideas on how to use any other state management I would appreciate it
class SignInDetailsModel with ChangeNotifier { String Username = ""; String Userpass =""; void signIn(String userName, String password) {
Username = userName;
Userpass = password;
print("The logged in user is ${Username}");
notifyListeners();}}
signIn(String username, String password) async {
DialogBuilder(context).showLoadingIndicator(
"Please wait as we authenticate you", "Authentication");
Map data = {'username': username, 'password': password};
var jsonResponse;
var response = await http.post(
Uri.parse("php script "),
body: data);
print("\n ${response.body} \n");
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body);
print(response.body);
if (jsonResponse != null) {
setState(() {
DialogBuilder(context).hideOpenDialog();
});
int isRegistered = jsonResponse['code'];
if (isRegistered == 1) { //correct password
Navigator.push(context, MaterialPageRoute(
builder: (BuildContext context) => HomeNav()));
} else {
scaffoldMessenger.showSnackBar(
mySnackBar("Wrong Password"),
);
}
}
} else {
setState(() {
DialogBuilder(context).hideOpenDialog();
});
}
}
Solution 1:[1]
General ideal to use textcontroller text via Provider
class ModelClass extend ChangeNotifier{
TextEditingController textEditingController= TextEditingController();
}
/// your class where you have textfeild and ChangeNotifierProvider/consumer
TextFeild(
controller: model.textEditingController;
)
/// Where you want to use the textfeild value or Consumer class
Text(model.textEditingController.text);
For complete example of how to user textfeild controller in another screen via Provider follow the link
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | navidanchitrali |
