Milad-Akarie / form_field_validator

MIT License
121 stars 29 forks source link

NoSuchMethodError: invalid member on null: 'isNotEmpty' #3

Open xiaoyu-nus opened 4 years ago

xiaoyu-nus commented 4 years ago

Hi, I am using the RequiredValidator on a DropdownButtonFormField widget. When the validation returns false (no value is selected), an exception is thrown:

The following JSNoSuchMethodError was thrown while handling a gesture:
NoSuchMethodError: invalid member on null: 'isNotEmpty'

When the exception was thrown, this was the stack:
packages/form_field_validator/form_field_validator.dart 50:17                                                isValid
packages/form_field_validator/form_field_validator.dart 55:12                                                call
packages/flutter/src/widgets/form.dart 392:27                                                                [_validate]   
packages/flutter/src/widgets/form.dart 385:7                                                                 <fn>
packages/flutter/src/widgets/framework.dart 1233:30                                                          setState      
packages/flutter/src/widgets/form.dart 384:5                                                                 validate      
packages/flutter/src/widgets/form.dart 209:24                                                                [_validate]   
packages/flutter/src/widgets/form.dart 203:12                                                                validate
packages/flutter/src/material/ink_well.dart 772:19                                                           [_handleTap]  
packages/flutter/src/material/ink_well.dart 855:36                                                           <fn>
packages/flutter/src/gestures/recognizer.dart 182:24                                                         invokeCallbackpackages/flutter/src/gestures/tap.dart 522:11                                                                handleTapUp   
packages/flutter/src/gestures/tap.dart 282:5                                                                 [_checkUp]
packages/flutter/src/gestures/tap.dart 254:7                                                                 acceptGesture
packages/flutter/src/gestures/arena.dart 156:12                                                              sweep
packages/flutter/src/gestures/binding.dart 222:20                                                            handleEvent   
packages/flutter/src/gestures/binding.dart 198:14                                                            dispatchEvent 
packages/flutter/src/gestures/binding.dart 156:7
[_handlePointerEvent]
packages/flutter/src/gestures/binding.dart 102:7
[_flushPointerEventQueue]
packages/flutter/src/gestures/binding.dart 86:7
[_handlePointerDataPacket]
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/window.dart 592:13           _invoke1
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/window.dart 238:5
invokeOnPointerDataPacket
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 129:14
[_onPointerData]
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 479:16  <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 440:21  <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 210:16  <fn>

Handler: "onTap"
Recognizer:
  TapGestureRecognizer#f9bab

I'm running the application on flutter web. Here is my flutter doctor output:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 1.18.0-11.1.pre, on Microsoft Windows [Version 10.0.18362.836], locale en-SG)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 3.6)
[!] IntelliJ IDEA Ultimate Edition (version 2019.2)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.45.1)
[√] Connected device (2 available)

! Doctor found issues in 1 category.

My DropdownbuttonFormField code:

class RegistrationFormComponent extends StatelessWidget {
  static GlobalKey<FormState> _formKey;
  RegisterBloc registerBloc;
  int stage;

@override
  Widget build(BuildContext context) {
    _formKey = GlobalKey<FormState>(debugLabel: "register_key");
    int stage = registerBloc.state.stage;
    switch (stage) {
      case 0:
        return _form1();
      case 1:
        return _form2();
      default:
        return _form3();
    }
  }

Widget _form3() {
    return Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
          SizedBox(height: 30.0),
          _buildPositionTF(),
        ],
      ),
    );
  }

Widget _buildPositionTF() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text(
          'Position: ',
          style: TextStyle(
            fontFamily: 'OpenSans',
            fontSize: 16.0,
            color: Colors.black87,
          ),
        ),
        SizedBox(height: 10.0),
        Container(
          height: 60.0,
          child: DropdownButtonFormField<String>(
            isExpanded: true,
            value: (registerBloc.state as RegisterProgress).position,
            hint: Text(
              'Select your position',
            ),
            validator:
                RequiredValidator(errorText: "Please select your position."),
            icon: Icon(Icons.arrow_downward),
            iconSize: 24,
            onChanged: (newValue) => registerBloc.add(RegisterSavePositionEvent(
                newValue)), // add select Position event
            items: ((registerBloc.state as RegisterProgress).positions)
                .map<DropdownMenuItem<String>>((Object value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value as String),
              );
            }).toList(),
          ),
        ),
      ],
    );
  }

Here is my submit button code:

Widget _buildNextBtn(RegisterBloc registerBloc) {
    return HandCursor(
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 25.0),
        width: double.infinity,
        child: RaisedButton(
          elevation: 5.0,
          onPressed: () {
            print("next button pressed");
            if (!(RegistrationFormComponent.getFormKey()
                    as GlobalKey<FormState>)
                .currentState
                .validate()) {
              return;
            }
            ...

I'm not sure whether this is the validator package issue or flutter issue or my own issure. Could you please help me?

ayberkcal commented 3 years ago

I have faced same issue. isValid method called before null check. I am not sure too why isValid method called before null check. https://github.com/Milad-Akarie/form_field_validator/blob/4c33b420ea07bf57369a258f006b4f42df7c0ab7/lib/form_field_validator.dart#L55