Flutter_Pinput icon indicating copy to clipboard operation
Flutter_Pinput copied to clipboard

[Feature Request] Can make the validator function become a async function?

Open cogivn opened this issue 1 year ago • 2 comments
trafficstars

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);
                },
              ),

cogivn avatar Jun 26 '24 03:06 cogivn

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 :)

andrijailic avatar Jun 27 '24 00:06 andrijailic

Hey, The Form widget which is the default way of creating forms in Flutter doesn't support async validator, so I have these options:

  1. Remove support for validating Pinput by Form and make the validator method async
  2. Create a new PinputForm widget which I don't like because more code = more confusion, more issue
  3. Use forceErrorState to show the error message anytime you want, you can use bloc, riverpod etc and it isn't as painful as other options.

Tkko avatar Dec 08 '24 11:12 Tkko