4

I added a float button in my application while clicking on it two text-field appear , without adding formKey the button works but when i use it to controle if the field text are empty or not by if statement if (formKey.currentState!.validate()) in the Debug Console i get this error

════════ Exception caught by gesture ═══════════════════════════════════════════
Unexpected null value.
═══════════════════════════════════════════════════════════════════════════════

this the code of floating button

 floatingActionButton: FloatingActionButton(
          onPressed: () {
            if (isBottomSheetShown) {
              if (formKey.currentState!.validate()) {
                Navigator.pop(context);
                isBottomSheetShown = false;
                setState(() {
                  fabIcon = Icons.edit;
                });
              }
            } else {
              scaffoldKey.currentState!.showBottomSheet(
                (context) => Container(
                  color: Colors.grey[100],
                  padding: const EdgeInsets.all(20),
                  child: Form(
                    key: formKey,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        TextField(
                          controller: titleController,
                          decoration: InputDecoration(
                            prefixIcon: Icon(Icons.title),
                            hintText: 'write a title',
                            labelText: 'Title',
                            border: OutlineInputBorder(),
                          ),
                        ),
                        SizedBox(
                          height: 15,
                        ),
                        TextField(
                          controller: timeController,
                          decoration: InputDecoration(
                            prefixIcon: Icon(Icons.watch_later_outlined),
                            hintText: 'write a time',
                            labelText: 'Time',
                            border: OutlineInputBorder(),
                          ),
                          onTap: () {
                            showTimePicker(
                              context: context,
                              initialTime: TimeOfDay.now(),
                            ).then((value) {
                              timeController.text =
                                  value!.format(context).toString();
                              print(value!.format(context));
                            });
                          },
                        ),
                      ],
                    ),
                  ),
                ),
              );
            }
            isBottomSheetShown = true;
            setState(() {
              fabIcon = Icons.add;
            });
          },
          child: Icon(
            fabIcon,
          )),

1 Answer 1

1

The Unexpected null value is occurring because you're asserting that formKey.currentState is not null before you validate it, so if currentState equals null, it will throw that error.

I'd suggest first checking if formKey.currentState != null before calling the validate method.

Sign up to request clarification or add additional context in comments.

1 Comment

I can just click one time on the floating button . Once i click one time , the text field does not appear if i repeat the clicking

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.