Flutter_Pinput
Flutter_Pinput copied to clipboard
[Feature Request] Can make the validator function become a async function?
Hi,
Thank you for create an awesome package for flutter developer.
I am facing the problem is try to verify the otp code from server side but now the Pinput's validator only accept synchronize function
So, I open a ticket to request new feature that validator field can receive a async function or new mechanism to verify the OTP code from server side.
Pinput.builder(
length: 6,
controller: controller,
focusNode: focusNode,
autofocus: true,
forceErrorState: state.status.isError,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
onCompleted: context.read<SmsCubit>().verify,
onChanged: context.read<SmsCubit>().inputting,
hapticFeedbackType: HapticFeedbackType.lightImpact,
pinputAutovalidateMode: PinputAutovalidateMode.disabled,
validator: (pin) async => verifyFromServer(pin),
separatorBuilder: (_) => const SizedBox(width: 1),
builder: (_, pinState) {
return _PinItem(state: pinState);
},
),
I got here for similar problem, but it seems like there is a workaround -> https://github.com/Tkko/Flutter_Pinput/issues/136.
You can supply onCompleted with async function and then, if validation on server fails, force this widget to go in error sate. I guess you should also set an errorText in that case and completely omit validator field.
I am not that happy with this solution (asyncValidator would be more elegant), but hey - it is what it is :)
Hey, The Form widget which is the default way of creating forms in Flutter doesn't support async validator, so I have these options:
- Remove support for validating
PinputbyFormand make the validator method async - Create a new
PinputFormwidget which I don't like because more code = more confusion, more issue - Use
forceErrorStateto show the error message anytime you want, you can use bloc, riverpod etc and it isn't as painful as other options.