0

I use widget StatefulWidget and _inputFiled

I've created a model class

class Profile {
  String? email;
  String? username;
  String? password;

  Profile({
    this.email,
    this.username,
    this.password,
  });
}

And using like

class _RegisterScreenState extends State<RegisterScreen> {
  final formKey = GlobalKey<FormState>();
  Profile profile = Profile();

  @override
  Widget build(BuildContext context) {
    return Form(
      child: Scaffold(
        body: Container(
          margin: EdgeInsets.all(24),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
             // _header(context),
              _inputField(context),
             // _forgotPassword(context),
            ],
          ),
        ),
      ),
    );
  }
 _inputField(context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        TextFormField(
          keyboardType: TextInputType.emailAddress,
          onSaved: (String? email) {
            formKey.currentState?.save();
            profile.email = email!;
          },
        ),
        SizedBox(height: 10),
        TextFormField(
          onSaved: (String? username) {
            formKey.currentState?.save();
            profile.username = username!;
          },
        ),
        SizedBox(height: 10),
        TextFormField(
          onSaved: (String? password) {
            profile.password = password!;
          },
        ),
        SizedBox(height: 10),
        ElevatedButton(
          onPressed: () {
            formKey.currentState?.save(); // Save the entire form
            print(
                "email =${profile.email} username =${profile.username} password =${profile.password}");
            Navigator.push(context, MaterialPageRoute(builder: ((context) {
              return LoginScreen();
            })));
          },
          child: Text(
            "Register",
          ), 
        )
      ],
    );
  }

Terminal say : email =null username =null password =null

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
JInSeok
  • 1
  • 2
  • 1
    Can you include full sample widget that will produce the error. The `profile` declaration and use cases are needed to check the error – Md. Yeasin Sheikh Dec 17 '22 at 16:06
  • 1
    Can you show the inputField code? – Abu Hurairah Dec 17 '22 at 16:09
  • _inputField(context) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ TextFormField( decoration: InputDecoration( hintText: "Email", border: OutlineInputBorder( borderRadius: BorderRadius.circular(18), borderSide: BorderSide.none), fillColor: Theme.of(context).primaryColor.withOpacity(0.1), filled: true, prefixIcon: Icon(Icons.email)), keyboardType: TextInputType.emailAddress, onSaved: (String? email) { formKey.currentState?.save(); profile.email = email!; }, – JInSeok Dec 17 '22 at 16:16
  • In my git https://github.com/devjinx/appshopping – JInSeok Dec 17 '22 at 16:18
  • 1
    https://stackoverflow.com/questions/52908453/flutter-textformfield-onsave-doesnt-get-called-after-successful-validation – Claudio Castro Dec 17 '22 at 16:29

3 Answers3

0

The form key hasn't been used.

  Widget build(BuildContext context) {
    return Form(
      key: formKey, // add here
      child: Scaffold(
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

You need put your Global key in your form:

  return Form(
      key: formKey,
      child: Scaffold(
 

And the formKey.currentState?.save() only on

 ElevatedButton(
          onPressed: () {
            formKey.currentState?.save(); // Save the entire form
            print(
                "email =${profile.email} username =${profile.username} password =${profile.password}");

it is not needed in onSave methods as it will be executed first on button onPressed

Claudio Castro
  • 1,541
  • 12
  • 28
0

It is becauae you made them null able and if you dont initialize them the first value will be null.

  • Instead of providing the answer directly, try writing a comment explaining the solution if it's not too lengthy. @Erfan Karimi – DSDmark Dec 20 '22 at 04:58