1

I am using this dart package otp_text_field 1.1.1.

                     OTPTextField(
                        length: 6,
                        width: MediaQuery.of(context).size.width,
                        fieldWidth: 30,
                        style: TextStyle(fontSize: 20),
                        textFieldAlignment: MainAxisAlignment.spaceAround,
                        fieldStyle: FieldStyle.underline,
                        onChanged: (pin) {
                          if (pin.length == 6) {
                            print("Changed: " + pin);
                          }
                        },
                      )

I get this error on the onChanged handler

The following _CastError was thrown while calling onChanged:
Null check operator used on a null value
When the exception was thrown, this was the stack

Not getting any idea on whats wrong.

Added Stacktrace -

  _OTPTextFieldState.buildTextField.<anonymous closure>
package:otp_text_field/otp_field.dart:179
#1      EditableTextState._formatAndSetValue
package:flutter/…/widgets/editable_text.dart:2298
#2      EditableTextState.updateEditingValue
package:flutter/…/widgets/editable_text.dart:1749
#3      TextInput._handleTextInputInvocation
package:flutter/…/services/text_input.dart:1351
#4      MethodChannel._handleAsMethodCall
package:flutter/…/services/platform_channel.dart:435
4
  • It seems the problem relies inside the package. Can you share the stacktrace (the lines below "this was the stack")? Commented Jul 16, 2021 at 23:35
  • @enzo I have added the stacktrace. Commented Jul 16, 2021 at 23:40
  • Also if I use onCompleted handler with onChanged I dont see any errors Commented Jul 16, 2021 at 23:45
  • Yes, the stacktrace points to this line in the package's source code. It only raises when onCompleted is null (i.e. you don't pass it as parameter). A related issue. Commented Jul 16, 2021 at 23:46

2 Answers 2

2
onChanged: (String? pin) {
  if (pin?.length == 6) { }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just provide the onCompleted callback to that widget. It is a nullable callback which means its not mandatory to pass that function. But this dependencies call this callback if the length of the top text field matches with the specified length you had provided.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.