Search code examples
flutterfirebaseloopscontrollertexteditingcontroller

The operator '[]' isn't defined for the type 'TextEditingController'


for(i= 0; i< 6 ; i++)
Pinput(length: 6,controller: model.otpController[i],
focusNode:focusNode,
androidSmsAutofillMethod:AndroidSmsAutofillMethod.smsUserConsentApi,
listenForMultipleSmsOnAndroid: true,
defaultPinTheme: defaultPinTheme,
hapticFeedbackType: HapticFeedbackType.lightImpact,
onCompleted: (pin) {debugPrint('onCompleted: $pin');},
onChanged: (value) {debugPrint('onChanged: $value');},

I try to count the number of wrong otp attempts , so i use for loop and int otpContrller[i] Please try to resolve this issue


Solution

  • Try this for full OTP attempts:

    int attempts = 0;
    
    Pinput(
        onCompleted: (pin) {
          String actualPin = '9999';
          if (pin == actualPin) {
            attempts = 0;
            print('success');
            return;
          } else {
            print('failed');
            setState(() {
              attempts += 1;
            });
          }
          print(pin);
        },
      ),
      Text('No of Attempts $attempts'),