iamvivekkaushik/OTPTextField

Null check operator used on a null value

Closed this issue · 9 comments

When onChanged is not passed then this error is thrown .After passing onChanged it works fine.

Null check operator used on a null value

This is the stack

When the exception was thrown, this was the stack:
#0 _OTPTextFieldState.buildTextField. (package:otp_text_field/otp_field.dart:156:27)
#1 EditableTextState._formatAndSetValue (package:flutter/src/widgets/editable_text.dart:2276:27)
#2 EditableTextState.textEditingValue= (package:flutter/src/widgets/editable_text.dart:2474:5)
#3 RenderEditable._handleDelete (package:flutter/src/rendering/editable.dart:877:27)
#4 RenderEditable._handleKeyEvent (package:flutter/src/rendering/editable.dart:550:7)

══╡ EXCEPTION CAUGHT BY WIDGETS ╞═══════════════════════════════════════════════════════════════════
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:
#0      _OTPTextFieldState.buildTextField.<anonymous closure> (package:otp_text_field/otp_field.dart:183:27)
#1      EditableTextState._formatAndSetValue (package:flutter/src/widgets/editable_text.dart:2298:27)
#2      EditableTextState.userUpdateTextEditingValue (package:flutter/src/widgets/editable_text.dart:2505:5)
#3      RenderEditable._setTextEditingValue (package:flutter/src/rendering/editable.dart:563:27)
#4      RenderEditable._handleDelete (package:flutter/src/rendering/editable.dart:1851:5)
#5      RenderEditable._handleKeyEvent (package:flutter/src/rendering/editable.dart:676:7)
#6      RawKeyboard._handleKeyEvent (package:flutter/src/services/raw_keyboard.dart:675:17)
#7      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:73:49)
#8      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:72:47)
#9      _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:284:33)
#10     _invoke3.<anonymous closure> (dart:ui/hooks.dart:223:15)
#14     _invoke3 (dart:ui/hooks.dart:222:10)
#15     PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:520:7)
#16     _dispatchPlatformMessage (dart:ui/hooks.dart:90:31)
(elided 3 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════════════════════════

Even when erasing OTP gives error

Another exception was thrown: Null check operator used on a null value

Another exception was thrown: Null check operator used on a null value

Same problem with onCompleted and it throws this error on every pin input!

I have opened a pull request (#14) regarding the solution to this problem.
You can change the file according to the comment of the pull request until author merges the pull request.

I'm also facing the same issue , throws the error on every pin input. is there any update on this

this error cuz onChange method will work even if you didn't use it so just you can put it without any functionality just as fast solve like this :

onChanged: (s) {},

i wish if that's help you .

I think @mohamed97alsaee is right!

This solution works for me properly.

OTPTextField(
length: 4,
otpFieldStyle: OtpFieldStyle(
backgroundColor: Colors.grey[200]!,
borderColor: Colors.white,
),
width: ScreenSize.width,
fieldWidth: 55,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
),
textFieldAlignment: MainAxisAlignment.spaceAround,
fieldStyle: FieldStyle.box,
onChanged: (val) {
setState(() {
pin = val;
});
print(pin);
},
onCompleted: (_) {},
),

Also fixed in #27