federico D'Armini
federico D'Armini

Reputation: 311

Why I can't validate and save my Form? (Flutter)

I created a Form with some TextFormField inside but can't save or validate it, this is the error :

The getter 'currentState' was called on null.
Receiver: null
Tried calling: currentState

When the exception was thrown, this was the stack
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      _HomePageState._validateAndSaveForm
package:lib/Form/homepage.dart:119

I think the problem is linked to the formKey .

This is the code, I copy-pasted it and it worked let me know if you have problems while reproducing the problem :

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  GlobalKey<FormState> formKey;
  String nomeF;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: buildContents(),//Just some wraps to better read the code
    );
  }

  Widget buildContents() { // UI that handles the Form
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Card(
          elevation: 0,
          color: Colors.blue,
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: buildForm(),
          ),
        ),
      ),
    );
  }

  Form buildForm() { //Form declaration
    return Form(
      key: formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: buildFormChildren(),
      ),
    );
  }

  List<Widget> buildFormChildren() { //Form content and save button
    return [
      TextFormField(
        style: TextStyle(
          color: Colors.white,
        ),
        decoration: InputDecoration(
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(
              color: Colors.white,
            ),
          ),
          focusedBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.white),
          ),
          labelText: "Name (REQUIRED)",
          hintText: "Inserisci nome",
          labelStyle: TextStyle(
            color: Colors.white,
          ),
        ),
        onSaved: (nomeUtente) => nomeF = nomeUtente,
        validator: (value) => value.isNotEmpty ? null : "Name can'\t be empty",
      ),
      ElevatedButton(
        style: ElevatedButton.styleFrom(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(18.0),
            ),
            primary: Colors.grey),
        child: Center(
          child: Text(
            "Save",
            style: TextStyle(color: Colors.white),
          ),
        ),
        onPressed: () => submit(),
      ),
    ];
  }

  //These functions save the form :

  void submit() {

    if (_validateAndSaveForm()) {
    }
  }

  bool _validateAndSaveForm() {
    final form = formKey.currentState;
    if (form.validate()) {
      form.save();
      return true;
    } else
      return false;
  }
}

Upvotes: 0

Views: 392

Answers (1)

Reza M
Reza M

Reputation: 573

You should initial formKey. Change this line GlobalKey<FormState> formKey; to GlobalKey<FormState> formKey = GlobalKey<FormState>();

Upvotes: 3

Related Questions