Want to properly pre-fill flutter form field? Figured it out: switched from TextFormField to TextField, created local variables to store changes to the fields, and used initState to set initial form values instead of doing it in build.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import 'dart:async'; import 'package:flutter/material.dart'; import '../models/user.dart'; class EditProfileView extends StatefulWidget { @override _EditProfileViewState createState() => new _EditProfileViewState(); } class _EditProfileViewState extends State<EditProfileView> { final GlobalKey<FormState> _formKey = new GlobalKey<FormState>(); void _handleSubmitted() { final FormState form = _formKey.currentState; if (!form.validate()) { _autovalidate = true; // Start validating on every change. showInSnackBar('Please fix the errors in red before submitting.'); } else { showInSnackBar('snackchat'); User.instance.first_name = firstName; User.instance.last_name = lastName; User.instance.save().then((result) { print("Saving done: ${result}."); }); } } // controllers for form text controllers final TextEditingController _firstNameController = new TextEditingController(); String firstName = User.instance.first_name; final TextEditingController _lastNameController = new TextEditingController(); String lastName = User.instance.last_name; @override void initState() { _firstNameController.text = firstName; _lastNameController.text = lastName; return super.initState(); } @override Widget build(BuildContext context) { final ThemeData themeData = Theme.of(context); final DateTime today = new DateTime.now(); return new Scaffold( appBar: new AppBar(title: const Text('Edit Profile'), actions: <Widget>[ new Container( padding: const EdgeInsets.fromLTRB(0.0, 10.0, 5.0, 10.0), child: new MaterialButton( color: themeData.primaryColor, textColor: themeData.secondaryHeaderColor, child: new Text('Save'), onPressed: () { _handleSubmitted(); Navigator.pop(context); }, )) ]), body: new Form( key: _formKey, autovalidate: _autovalidate, onWillPop: _warnUserAboutInvalidData, child: new ListView( padding: const EdgeInsets.symmetric(horizontal: 16.0), children: <Widget>[ new Container( child: new TextField( decoration: const InputDecoration(labelText: "First Name", hintText: "What do people call you?"), autocorrect: false, controller: _firstNameController, onChanged: (String value) { firstName = value; }, ), ), new Container( child: new TextField( decoration: const InputDecoration(labelText: "Last Name"), autocorrect: false, controller: _lastNameController, onChanged: (String value) { lastName = value; }, ), ), ], ))); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.