Pages on Navigator stack rebuild when a new page is pushed in Flutter Pages on Navigator stack rebuild when a new page is pushed in Flutter dart dart

Pages on Navigator stack rebuild when a new page is pushed in Flutter


I solved the problem by simply changing the class like this :

import 'package:flutter/material.dart';class UserLoader extends StatefulWidget {  @override  _UserLoaderState createState() => new _UserLoaderState();}class _UserLoaderState extends State<UserLoader> {  Widget _form; // Save the form  @override  Widget build(BuildContext context) {    if (_form == null) { // Create the form if it does not exist      _form = _createForm(context); // Build the form    }    return _form; // Show the form in the application  }  Widget _createForm(BuildContext context) {    // This is the exact content of the build method in the question    final _formKey = new GlobalKey<FormState>();    final _emailController = new TextEditingController();    return new Scaffold(        appBar: new AppBar(          title: new Text("Informations"),          actions: <Widget>[            new IconButton(                icon: const Icon(Icons.save),                onPressed: () {                  // unrelated stuff happens here                })          ],        ),        body: new Center(          child: new SingleChildScrollView(              child: new Form(                  key: _formKey,                  child: new Column(children: <Widget>[                    new ListTile(                      leading: const Icon(Icons.email),                      title: new TextFormField(                        decoration: new InputDecoration(                          hintText: "Email",                        ),                        keyboardType: TextInputType.emailAddress,                        controller: _emailController,                        validator: _validateEmail,                      ),                    ),                  ]))),        ));    }  }}

Hope this may help someone else someday.


All you need to do is move this row

final _formKey = new GlobalKey<FormState>();

from build method to state class declaration (i.e. outside from build). Key must be created once when class is created. In your case the key is re-created each build action.