Showing Error Text on TextField in ShowDialog - flutter

I am trying to build an AlertDialog to reauthenticate user before deleting account. I would like to show error text when reauthentication does not work.
The errors are handled correctly as I can see by using print() statements. However I cannot get the TextField to show the error as a feedback to the user. I think it must be something to do with the state of the SimpleDialog itself, rather than the state of the outer widget...
Here is the code:
Future _validate({String email, String password}) async {
setState(() {
_wrongEmail = false;
_wrongPassword = false;
_emailErrorText = null;
_passwordErrorText = null;
});
try {
if (await userDataBase.checkCorrectEmail(
email: email, loggedInUser: widget.user.id)) {
throw FirebaseAuthException(
message: 'email is not correct', code: 'user-not-found');
}
await userDataBase.authenticateUser(email, password);
} catch (e) {
if (e.code == 'wrong-passwod') {
setState(() {
_passwordErrorText = 'Password not Correct';
});
} else if (e.code == 'user-not-found') {
setState(() {
_emailErrorText = 'Email not Correct';
});
} else {
setState(() {
_emailErrorText = 'Email not Correct';
_passwordErrorText = 'Password not Correct';
_wrongPassword = true;
_wrongEmail = true;
});
}
throw e;
}
}
Future _authenticate({String email, String password}) async {
try {
await _validate(email: email, password: password);
} catch (e) {
print(e.code);
print(_wrongEmail);
print(_emailErrorText);
return;
}
print('correct behaviour');
// Navigator.pop(context);
// setState(() {
// _loading = true;
// });
// await userDataBase.deleteUser(widget.user.id);
// Navigator.of(context).popUntil((route) => route.isFirst);
}
Future _confirmIdentity() {
final emailController = TextEditingController();
final passwordController = TextEditingController();
_emailErrorText = null;
_passwordErrorText = null;
return showDialog(
context: context,
builder: (context) => AlertDialog(
content: Container(
height: 200,
child: Column(
children: [
Text('Confirm Your Identity'),
TextField(
controller: emailController,
decoration: InputDecoration(
errorText: _wrongEmail ? _emailErrorText : null,
labelText: 'email'),
),
TextField(
controller: passwordController,
decoration:
InputDecoration(
errorText: _wrongPassword ? _passwordErrorText : null,
labelText: 'password'
),
),
Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
onPressed: () {
_authenticate(
email: emailController.text,
password: passwordController.text);
},
child: Text('Confirm'),
),
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
),
],
)
],
),
),
),
);
}
Thank you. I can post all the widget code ecc. if you need it: it is a statefull widget.

For 1st 2 if conditions, you are setting the error message, but not setting the respective boolean flags to true.
Example:
if (e.code == 'wrong-passwod') {
setState(() {
_passwordErrorText = 'Password not Correct';
});
}
Here, in the setState()function, you will also need to set _wrongPassword = true;

I fixed the issue. The boolean variables _wrongPassword, _wrongEmail are not necessary seen as when the _PasswordErrorText or _EmailErrorText are null, the error text is not displayed by default.
It is necessary to build a statefull widget that is the content of ShowDialog! then everything works.

Related

Singin request with firebase is not working

I'm having a problem trying to do a login validation with firebase. Coincidentally I can create a new firebase account register it and I can enter my application. but when I go back to the home screen, I can't log in with that registered account. What do you think is the mistake I'm making?.
The console returns this to me.
Error image
my screen code
class AuthScreen extends StatefulWidget {
const AuthScreen({Key? key}) : super(key: key);
#override
State<AuthScreen> createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
final _auth = FirebaseAuth.instance;
var _isLoading = false;
void _submitAuthForm(
String email,
String userName,
String password,
bool isLogin,
BuildContext ctx,
) async {
UserCredential authResult;
try {
setState(() {
_isLoading = true;
});
if (isLogin) {
authResult = await _auth.signInWithEmailAndPassword(
email: email, password: password);
} else {
authResult = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
await FirebaseFirestore.instance
.collection('users')
.doc(authResult.user!.uid)
.set({
'username': userName,
'email': email,
});
}
} on PlatformException catch (err) {
var message = 'An error ocurred, Please check your credentials';
if (err.message != null) {
message = err.message!;
}
Scaffold.of(ctx).showSnackBar(SnackBar(
content: Text(message),
backgroundColor: Theme.of(ctx).errorColor,
));
setState(() {
_isLoading = false;
});
} catch (err) {
print(err);
setState(() {
_isLoading = false;
});
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).primaryColor,
body: AuthForm(_submitAuthForm, _isLoading),
);
}
}
authentication code
class AuthForm extends StatefulWidget {
AuthForm(this.submitFn, this.isLoading);
final bool isLoading;
final void Function(String email, String userName, String password,
bool isLogin, BuildContext ctx) submitFn;
#override
State<AuthForm> createState() => _AuthFormState();
}
class _AuthFormState extends State<AuthForm> {
final _formKey = GlobalKey<FormState>();
var _isLogin = true;
var _userEmail = '';
var _userName = '';
var _userPassword = '';
void _trySubmit() {
final isValid = _formKey.currentState!.validate();
FocusScope.of(context).unfocus();
if (isValid) {
_formKey.currentState!.save();
widget.submitFn(
_userEmail.trim(),
_userPassword.trim(),
_userName.trim(),
_isLogin,
context,
);
}
}
#override
Widget build(BuildContext context) {
return Center(
child: Card(
margin: EdgeInsets.all(20),
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
key: ValueKey('email'),
validator: (value) {
if (value!.isEmpty ||
!value.contains('#') ||
!value.contains('.com')) {
return 'Please enter a valid email';
}
return null;
},
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(labelText: 'Email address'),
onSaved: (value) {
_userEmail = value!;
},
),
if (!_isLogin)
TextFormField(
key: ValueKey('username'),
validator: (value) {
if (value!.isEmpty || value.length < 4) {
return 'Please enter at least 4 characters';
}
return null;
},
decoration: InputDecoration(labelText: 'Username'),
onSaved: (value) {
_userName = value!;
},
),
TextFormField(
key: ValueKey('password'),
validator: (value) {
if (value!.isEmpty || value.length < 7) {
return 'Password must be at least 7 characters long';
}
return null;
},
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
onSaved: (value) {
_userPassword = value!;
},
),
SizedBox(
height: 15,
),
if (widget.isLoading) CircularProgressIndicator(),
if (!widget.isLoading)
ElevatedButton(
onPressed: _trySubmit,
child: Text(_isLogin ? 'Login' : 'Signup'),
),
if (!widget.isLoading)
TextButton(
onPressed: () {
setState(() {
_isLogin = !_isLogin;
});
},
child: Text(_isLogin
? 'Create new account'
: 'I already have a account'))
],
)),
),
),
));
}
}

How to make Speech To Text stop when the user stop talking?

I'm trying to make an icon in the textfield that is supposed to stop and convert into mic_none when the user has finished saying the text , but that it doesn't happen.
What happens is that the text reception stops, but the icon does not return to its outliend form, but rather I have to click on it to convert it into mic_none.
I would appreciate any help from you
TextEditingController? _directionController;
String text = "";
bool isListening = false;
bool isListening1 = false;
#required
Function(String text)? onResult;
#required
ValueChanged<bool>? onListening;
static final _speech = SpeechToText();
void toggleRecording() async {
if (!isListening1) {
bool isAval = await _speech.initialize(
onStatus: (status) => onListening!(_speech.isListening),
onError: (e) => print('Error: $e'),
);
if (isAval) {
setState(() {
isListening1 = true;
});
// it is for recognaization
_speech.listen(
onResult: (value) => setState(() {
_directionController!.text = value.recognizedWords;
addRecipe.userDirections[widget.index] =
value.recognizedWords;
// onResult!(value.recognizedWords);
}));
}
} else {
setState(() {
isListening1 = false;
_speech.stop();
});
}
}
Here is the build of my code:
Widget build(BuildContext context) {
// run this method when the interface has been loaded
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
_directionController!.text = addRecipe.userDirections[widget.index] ?? '';
});
return TextFormField(
controller: _directionController,
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: toggleRecording,
icon: Icon(
isListening1 ? Icons.mic : Icons.mic_none,
color: Color(0xFFeb6d44),
),
),
hintText: 'Enter a direction'), // errorText: _errorText
onChanged: (value) {
addRecipe.userDirections[widget.index] = _directionController!.text;
// setState(() {}); //used to refresh the screen //OLD
},
validator: (value) {
if (value!.trim().isEmpty) return 'Please enter a direction';
return null;
},
);
}
You can use _speech.isListening instead of your isListening1
Icon(
_speech.isListening? Icons.mic : Icons.mic_none,
color: Color(0xFFeb6d44),
),

Snackbar showing twice due to stacked screen Flutter, How can i avoid it?

I don't want to show Snackbar on stacked screen. When user taps on Signup from LoginScreen. Then, SignUpScreen stacked over LoginScreen. but the problem is both implements same ProviderListener and therefore, It shows Snackbar multiple times. How can I avoid it? , How can I make sure that, If current route is SignUp then show Snackbar
UserAuthService.dart
import 'package:notifications/domain/repository/firebase_repository/firebase_user_repo.dart';
import 'package:notifications/domain/services/auth_service/all_auth_builder.dart';
import 'package:notifications/export.dart';
import 'package:notifications/resources/local/local_storage.dart';
enum AuthenticationStatus {
loading,
error,
success,
}
class UserAuthService extends ChangeNotifier {
final authBuilder = AllTypeAuthBuilder();
String? _errorMsg, _sessionID;
AuthenticationStatus _status = AuthenticationStatus.loading;
EmailLinkAuthenticationRepo? _repo;
String? get errorMsg => this._errorMsg;
void get _setDefault {
_errorMsg = null;
//_sessionID = null;
}
String? get sessionID {
return LocallyStoredData.getSessionID();
}
void logOut() {
return LocallyStoredData.deleteUserKey();
}
Future<bool> userExists(String userID) async {
final isExists = await authBuilder.checkUserExists(userID);
return isExists ? true : false;
}
Future<bool> login() async {
_setDefault;
try {
await authBuilder.login();
return true;
} on BaseException catch (e) {
log("Exception $e");
_errorMsg = e.msg;
notifyListeners();
return false;
}
}
Future<bool> register(String username, String email, String password) async {
_setDefault;
try {
await authBuilder.register(username, email, password);
return true;
} on BaseException catch (e) {
log("Exception $e");
_errorMsg = e.msg;
notifyListeners();
return false;
}
}
Future<bool> signIn(String userID, String password) async {
_setDefault;
try {
await authBuilder.signIn(userID, password);
return true;
} on BaseException catch (e) {
log("Exception ${e.msg}");
_errorMsg = e.msg;
notifyListeners();
}
return false;
}
void loginWithEmail(String email) async {
_setDefault;
try {
_repo = await authBuilder.loginWithEmail(email);
_repo!.onLinkListener(
onSuccess: _onSuccess,
onError: _onError,
);
} on BaseException catch (e) {
log("Exception ${e.msg}");
_errorMsg = e.msg;
}
notifyListeners();
}
Future<bool> _onSuccess(PendingDynamicLinkData? linkData) async {
_setDefault;
try {
log("OnLinkAuthenticate");
await _repo!.onLinkAuthenticate(linkData);
return true;
} on BaseException catch (e) {
log("Error onSucess: $e");
_errorMsg = e.msg;
notifyListeners();
}
return false;
}
Future<dynamic> _onError(OnLinkErrorException? error) async {
log("Error $error in Link");
}
Future<void> tryTo(Function callback) async {
try {
await callback();
} on BaseException catch (e) {
_errorMsg = e.msg;
}
}
}
LoginScreen.dart
class LoginScreen extends StatefulWidget {
#override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _userIDController = TextEditingController(),
_passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool? isAuthenticated;
#override
initState() {
super.initState();
}
#override
void dispose() {
_userIDController.dispose();
_passwordController.dispose();
super.dispose();
}
_onGoogleLogin() async {
context.read(loginPod).login();
}
_onLoginButtonTap() {
networkCheckCallback(context, () async {
if (_formKey.currentState!.validate()) {
WidgetUtils.showLoaderIndicator(context, 'Loading...');
final isSignedIn = await context
.read(loginPod)
.signIn(_userIDController.text, _passwordController.text);
Navigator.pop(context);
if (isSignedIn) Beamer.of(context).beamToNamed(Routes.home);
}
});
}
_resetAuthenticateState() {
if (isAuthenticated != null)
setState(() {
isAuthenticated = null;
});
}
onUsernameChange(String? value) async {
final error = await hasNetworkError();
if (_userIDController.text.isNotEmpty && error == null) {
isAuthenticated = await context.read(loginPod).userExists(value!);
setState(() {});
return;
}
_resetAuthenticateState();
}
onPasswordChange(String? value) {
//Code goes here....
}
loginWith(BuildContext context, LoginType type) {
switch (type) {
case LoginType.emailLink:
Beamer.of(context).beamToNamed(Routes.email_link_auth);
break;
case LoginType.idPassword:
Beamer.of(context).beamToNamed(Routes.login_id_pass);
break;
case LoginType.googleAuth:
Beamer.of(context).beamToNamed(Routes.login_with_google);
break;
case LoginType.unknown:
Beamer.of(context).beamToNamed(Routes.register);
break;
}
}
Future<bool> _onBackPress(_) async {
return await showDialog<bool>(
context: _,
builder: (context) {
return AlertDialog(
title: Text("Do you want to exit?"),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text("OK")),
TextButton(
onPressed: () {
Beamer.of(_).popRoute();
},
child: Text("Cancel"))
],
);
}) ??
false;
}
_onLoginStatus(BuildContext _, UserAuthService service) {
if (service.errorMsg != null)
_.showErrorBar(
content: Text(
"WithLogin" + service.errorMsg!,
style: TextStyle(fontSize: 12.sp),
));
}
#override
Widget build(BuildContext _) {
return ProviderListener(
onChange: _onLoginStatus,
provider: loginPod,
child: WillPopScope(
onWillPop: () => _onBackPress(_),
child: Scaffold(
body: SingleChildScrollView(
child: SizedBox(height: 1.sh, child: _buildLoginScreen())),
),
),
);
}
Widget _buildLoginScreen() {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
//_buildVrtSpacer(60),
_buildHeading(),
//_buildVrtSpacer(30),
_buildForm(),
//_buildVrtSpacer(30),
_buildIconButtons(),
_buildSignUpButton(),
],
);
}
BoldHeadingWidget _buildHeading() =>
BoldHeadingWidget(heading: AppStrings.login);
ResponsiveVrtSpacer _buildVrtSpacer(double value) =>
ResponsiveVrtSpacer(space: value);
Widget _buildForm() {
return CustomForm(
formKey: _formKey,
child: Column(
children: [
_buildUsernameField(),
_buildVrtSpacer(10),
_buildPasswordField(),
_buildForgetPassword(),
_buildLoginButton(),
],
),
);
}
Widget _buildPasswordField() {
return CustomTextFieldWithLabeled(
controller: _passwordController,
label: AppStrings.password,
hintText: AppStrings.password,
onValidate: (String? value) =>
(value!.isEmpty) ? AppStrings.emptyPasswordMsg : null,
obscureText: true,
onChange: onPasswordChange,
icon: CupertinoIcons.lock);
}
Widget _buildUsernameField() {
return CustomTextFieldWithLabeled(
controller: _userIDController,
label: AppStrings.usernameOrEmail,
hintText: AppStrings.usernameOrEmail1,
icon: CupertinoIcons.person,
onChange: onUsernameChange,
onValidate: (String? value) =>
(value!.isEmpty) ? AppStrings.emptyUserIDMsg : null,
suffixIcon: isAuthenticated == null
? null
: (isAuthenticated!
? CupertinoIcons.checkmark_alt_circle_fill
: CupertinoIcons.clear_circled_solid),
suffixColor: isAuthenticated == null
? null
: (isAuthenticated! ? Colors.green : Styles.defaultColor));
}
Widget _buildIconButtons() {
return Column(
children: [
Text("Or", style: TextStyle(fontSize: 14.sp)),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(width: 10),
_buildIconButton(
iconPath: 'assets/icons/email-icon.svg', onTap: () {}),
const SizedBox(width: 8),
_buildIconButton(
iconPath: 'assets/icons/icons8-google.svg',
onTap: _onGoogleLogin),
],
),
],
);
}
Widget _buildIconButton(
{required String iconPath, required VoidCallback onTap}) {
return GestureDetector(
onTap: onTap,
child: SvgPicture.asset(
iconPath,
height: 30.sp,
width: 30.sp,
));
}
Widget _buildSignUpButton() {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
AppStrings.dontHaveAccount,
style: TextStyle(color: Colors.black54, fontSize: 14.sp),
),
const SizedBox(height: 5),
CustomTextButton(
title: "Sign Up",
onPressed: () => Beamer.of(context).beamToNamed(Routes.register)),
],
);
}
Widget _buildLoginButton() {
return DefaultElevatedButton(
onPressed: _onLoginButtonTap,
title: AppStrings.login,
);
}
Widget _buildForgetPassword() {
return Align(
alignment: Alignment.centerRight,
child: TextButton(
style: ButtonStyle(
overlayColor: MaterialStateProperty.all(Color(0x11000000)),
foregroundColor: MaterialStateProperty.all(Color(0x55000000)),
),
onPressed: () {},
child: Text("Forget Password?"),
),
);
}
}
SignUpScreen.dart
class SignUp extends StatefulWidget {
const SignUp({Key? key}) : super(key: key);
#override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
final _formKey = GlobalKey<FormState>();
final _usernameController = TextEditingController(text: "hammad11"),
_emailController = TextEditingController(text: "mason#gmail.com"),
_passwordController = TextEditingController(text: "ha11"),
_confirmPassController = TextEditingController(text: "ha11");
_onValidate(String? value, ValidationType type) {
switch (type) {
case ValidationType.username:
if (value!.isNotEmpty && value.length < 8)
return "Username Must Be 8 characters long";
else if (value.isEmpty) return "Username required";
return null;
case ValidationType.email:
if (value!.isEmpty)
return "Email required";
else if (!value.isEmail) return "Please enter a Valid Email";
return null;
case ValidationType.password:
if (value!.isEmpty)
return "Password required";
else if (value.isAlphabetOnly || value.isNumericOnly)
return "Password must be AlphaNumeric";
return null;
case ValidationType.confirmPassword:
if (value!.isEmpty)
return "Confirm Password required";
else if (value != _passwordController.text)
return "Password doesn't match";
return null;
}
}
_onRegister() async {
//Clears any snackbar opened due to Error or Multiple clicks
//ScaffoldMessenger.of(context).clearSnackBars();
log("SignUp -> _onRegisterTap ");
if (_formKey.currentState!.validate()) {
WidgetUtils.showLoaderIndicator(context, "Please wait! Loading.....");
final isLoggedIn = await context.read(loginPod).register(
_usernameController.text,
_emailController.text,
_passwordController.text,
);
await Beamer.of(context).popRoute();
if (isLoggedIn) Beamer.of(context).beamToNamed(Routes.login);
} else
log("Form Input Invalid");
}
_onChanged(_, UserAuthService service) async {
if (service.errorMsg != null) WidgetUtils.snackBar(_, service.errorMsg!);
// if (!service.isLoading) await Beamer.of(context).popRoute();
// if (service.taskCompleted) {
// log("User Added Successfully");
// Beamer.of(context).popToNamed(
// Routes.login_id_pass,
// replaceCurrent: true,
// );
// } else {
// WidgetUtils.snackBar(context, service.errorMsg!);
// }
}
_alreadyHaveAccount() => Beamer.of(context).popToNamed(Routes.main);
#override
Widget build(BuildContext context) {
return ProviderListener(
provider: loginPod,
onChange: _onChanged,
child: Scaffold(
body: LayoutBuilder(builder: (context, constraints) {
return SingleChildScrollView(
child: SizedBox(
height: 1.sh,
child: Column(
children: [
_buildSpacer(50),
BoldHeadingWidget(heading: "Sign Up"),
CustomForm(
child: Column(
children: [
CustomTextFieldWithLabeled(
label: "Username",
hintText: "Type Username",
onValidate: (value) =>
_onValidate(value, ValidationType.username),
controller: _usernameController,
icon: CupertinoIcons.person),
CustomTextFieldWithLabeled(
label: "Email",
hintText: "Type Email",
controller: _emailController,
onValidate: (value) =>
_onValidate(value, ValidationType.email),
icon: CupertinoIcons.envelope),
CustomTextFieldWithLabeled(
label: "Password",
hintText: "Type Password",
controller: _passwordController,
onValidate: (value) =>
_onValidate(value, ValidationType.password),
icon: CupertinoIcons.lock),
CustomTextFieldWithLabeled(
label: "Confirm Password",
hintText: "Type Confirm Password",
onValidate: (value) => _onValidate(
value, ValidationType.confirmPassword),
controller: _confirmPassController,
icon: CupertinoIcons.lock),
Row(
children: [
Checkbox(
value: true,
onChanged: (value) {},
materialTapTargetSize:
MaterialTapTargetSize.shrinkWrap,
),
Flexible(
child: Text(
"I accept Terms & Conditions and the Privacy Policy",
style: TextStyle(fontSize: 13.sp),
),
),
],
),
_buildSpacer(10),
DefaultElevatedButton(
title: "Sign Up",
onPressed: _onRegister,
),
],
),
formKey: _formKey,
),
const SizedBox(height: 10),
CustomTextButton(
onPressed: () => Beamer.of(context)
.popToNamed(Routes.login, stacked: false),
title: AppStrings.alreadyHaveAccount,
),
// Spacer(flex: 2),
],
),
),
);
}),
),
);
}
ResponsiveVrtSpacer _buildSpacer(double value) =>
ResponsiveVrtSpacer(space: value.h);
}
showsnackbar(String message, context) {
final snackbar = SnackBar(
content: Text(message),
duration: Duration(seconds: 2),
);
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(snackbar);
}
Use this function to call snackbar it will remove the current snackbar and will only show the latest.

How to reading OTP from firebase SMS automatically in flutter

I'm working on a code that asks for an OTP verification code to register the phone in firebase. It works fine manually but I want to make the code or app read the code sent in SMS from firebase and check it automatically.
For example, most applications when the verification code is requested and after the phone receives the verification code, the code is automatically written in the verification code registration boxes.
I searched for a long time, but it didn't work with me.
full code:
class _OtpScreenState extends State<OtpScreen> {
String phoneNo;
String smsOTP;
String verificationId;
String errorMessage = '';
final FirebaseAuth _auth = FirebaseAuth.instance;
bool showLoading = false;
void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) async {
setState(() {
showLoading = true;
});
try {
final authCredential = await _auth.signInWithCredential(phoneAuthCredential);
setState(() {
showLoading = false;
});
if (authCredential?.user != null) {
}
} on FirebaseAuthException catch (e) {
setState(() {
showLoading = false;
});
print(e.message);
}
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: SingleChildScrollView(
child: Container(
child: Column(
children: [
Container(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Row(
children: [
Expanded(
child: PinEntryTextField(
fields: 6,
onSubmit: (text) {
smsOTP = text as String;
},
),
),
],
),
GestureDetector(
onTap: () {
verifyOtp();
},
child: Container(
margin: const EdgeInsets.all(8),
height: 45,
width: double.infinity,
decoration: BoxDecoration(
color: CustomColors.Background,
borderRadius: BorderRadius.circular(0),
),
alignment: Alignment.center,
child: Text('Check'),
),
),
],
),
)
],
),
),
),
),
),
);
}
Future<void> generateOtp(String contact) async {
final PhoneCodeSent smsOTPSent = (String verId, [int forceCodeResend]) {
verificationId = verId;
};
try {
await _auth.verifyPhoneNumber(
phoneNumber: contact,
codeAutoRetrievalTimeout: (String verId) {
verificationId = verId;
},
codeSent: smsOTPSent,
timeout: const Duration(seconds: 60),
verificationCompleted: (AuthCredential phoneAuthCredential) {},
verificationFailed: (exception) {
print(exception);
});
} catch (e) {
handleError(e as PlatformException);
}
}
//Method for verify otp entered by user
Future<void> verifyOtp() async {
if (smsOTP == null || smsOTP == '') {
print('please enter 6 digit otp');
return;
}
try {
final AuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: smsOTP,
);
signInWithPhoneAuthCredential(credential);
} catch (e) {
handleError(e as PlatformException);
} }
void handleError(PlatformException error) {
switch (error.code) {
case 'ERROR_INVALID_VERIFICATION_CODE':
FocusScope.of(context).requestFocus(FocusNode());
setState(() {
errorMessage = 'Invalid Code';
});
print('error');
break;
default:
print('error');
break;
}
}
}
For IOS, SMS autofill is provided by default.
For android you can use this package.
Use this in your code:
String otpValue = credential.smsCode.toString();
Example from my code:

Flutter ChangeNotifierProvider issue

I have a ProperyListPage and on each list item clicked this is the action it does.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChangeNotifierProvider.value(
value: property,
child: PropertyPage(),
),
),
);
}
inside of PropertyPage I have BottomNavigationBar, when you click on Edit BottomNavigationBarItem then it goes to a PropertyEditTab like this
PropertyEditTab(),
inside of PropertyEditTab I have a Form widget like this
child: Form(
key: _formKey,
child: Column(
children: [
PropertyForm(
callback: saveProperty,
),
...
)...
My saveProperty callback looks like this
void saveProperty(BuildContext context) async {
if (_formKey.currentState.validate()) {
PropertyViewModel _propertyVM = Provider.of<PropertyViewModel>(context, listen: false);
savingProperty();
final isSaved = await _propertyVM.updateProperty();
if (isSaved) {
propertySaved();
} else {}
}
}
the updateProperty method does an update to Firestore like this
class PropertyViewModel extends ChangeNotifier {
...
Future<bool> updateProperty() async {
bool isSaved = false;
User user = _firebaseAuth.currentUser;
print(uid);
final property = Property(
user.email,
...
);
try {
await _firestore
.collection(Constants.propertyCollection)
.doc(property.uid)
.update(property.toMap());
isSaved = true;
} on Exception catch (e) {
print(e);
message = 'Property not saved.';
} catch (e) {
print(e);
message = 'Error occurred';
}
notifyListeners();
return isSaved;
}
...
}
then each one of my form fields look like this
FormTextField(
name: 'address_line_1',
hintText: 'Address Line 1',
initialValue: propertyVM.addressLine1 ?? '',
onChanged: (value) {
propertyVM.addressLine1 = value;
},
),
and this is my onPress of button to save property
ThemeButton(
text: 'Save Property',
buttonColor: Constants.kPrimaryColor,
textColor: Colors.white,
onPress: () => widget.callback(context),
),
the issue is that the original PropertyViewModel property isn't updating when notifyProviders() is called and I can't figure out what I am doing wrong.
Thanks in advance for the help.
we were writing on the slack channel.
I think the reason is because in the FormTextField widget change it to this:
FormTextField(
name: 'address_line_1',
hintText: 'Address Line 1',
initialValue: propertyVM.addressLine1 ?? '',
onChanged: (value) {
propertyVM.updateAdressLine(value);
},
),
And in your PropertyViewModel make a function
void updateAdressLine() {
adressLine1 = value;
notifyListeners();
}
It's because from your FormTextField you are only changing the value. You need pack the changes in a method so you can change the value and notifyListeners at one time.