Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve the error: Null check operator used on a null value?

Field validation does not work, an error is displayed: Null check operator used on a null value.
I took an example from docs.flutter. How can I solve this?

class _AddGroupPageState extends State<AddGroupPage> {
  final _groupController = TextEditingController();
  final _formKey = GlobalKey<FormState>();
  @override
  void dispose() {
    _groupController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return: Scaffold(
    body: Padding(
      padding: const EdgeInsets.all(8.0),
      child: Column(
        children: [
          TextFormField(
            key: _formKey,
            controller: _groupController,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Group name cannot be empty';
              }
              return null;
            },
          ),
        ],
      ),
    ),
    ...
void _saveGroup() {
  final isValid = _formKey.currentState!.validate();
  if (isValid) {
    BlocProvider.of<NewGroupBloc>(context)
        .add(AddGroupEvent(_groupController.text.trim()));
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Wrap your TextFormField with Form widget and pass the formkey to that widget

 Form(
          key: _formKey,
          child: TextFormField(
            controller: _groupController,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Group name cannot be empty';
              }
              return null;
            },
          ),
        ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading