Flutter 颤振:TextFormField的保存方法,不将值存储到变量

Flutter 颤振:TextFormField的保存方法,不将值存储到变量,flutter,dart,Flutter,Dart,我有一个表单,提示用户输入地址。表单被分解为多个TextFormField小部件,这些小部件在名为address\u widget.dart的文件中定义。这些小部件从文件中调用:AddressPage.dart。表单正确加载并得到验证,但是地址\u小部件中的onSaved方法保存的值。dart未存储在地址页.dart中的变量城市和地址中。打印变量城市和地址时,将打印空值。这是正确的方法还是我遗漏了什么 地址_widget.dart 地址页.省道 import'package:userprojec

我有一个表单,提示用户输入地址。表单被分解为多个TextFormField小部件,这些小部件在名为
address\u widget.dart
的文件中定义。这些小部件从文件中调用:
AddressPage.dart
。表单正确加载并得到验证,但是
地址\u小部件中的onSaved方法保存的值。dart
未存储在
地址页.dart
中的变量
城市
地址
中。打印变量
城市
地址
时,将打印空值。这是正确的方法还是我遗漏了什么

地址_widget.dart

地址页.省道

import'package:userproject/widgets/address_widget.dart';
类AddressPage扩展StatefulWidget{
@凌驾
_AddressPageState createState()=>\u AddressPageState();
}
类_AddressPageState扩展状态{
最终的GlobalKey _formKey=GlobalKey();
最终GlobalKey _scaffoldKey=GlobalKey();
字符串城市;
字符串地址;
@凌驾
小部件构建(构建上下文){
返回脚手架(
钥匙:_scaffoldKey,
正文:表格(
键:_formKey,
子:列(
儿童:[
正文(
'输入您的地址',
样式:TextStyle(
fontFamily:“蒙特塞拉特”,
fontWeight:fontWeight.bold,
),
建城(市),,
建筑地址(地址),
划船(
儿童:[
升起的按钮(
已按下:(){
如果(!\u formKey.currentState.validate()){
返回;
}
_formKey.currentState.save();
打印(“城市-$city”);//打印为空
打印(“地址-$address”);//打印为空
},
),
],
)
],
),
),
),
}
}

代码已简化,便于参考。

您必须使用
文本编辑控制器
从文本字段获取数据




class _AddressWidgetState extends State<AddressWidgetScreen> {
  
  final myController = TextEditingController();
//a string to store the input from the text field
  String city = '';

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
//in your case TextFormField
        TextField(
  controller: myController,
),
 RaisedButton(
            onPressed: () {
              //store the data from textfield to our new string
              city = myController.text.toString();
      //therest of your logic
            },
   
            textColor: Colors.white,),
                   

类_AddressWidgetState扩展状态

import 'package:userproject/widgets/address_widget.dart';

class AddressPage extends StatefulWidget {
  @override
  _AddressPageState createState() => _AddressPageState();
}

class _AddressPageState extends State<AddressPage> {
  
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  String city;
  String address;  
  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: Form(
               key: _formKey,
               child: Column(
                      children: <Widget>[
                        Text(
                          'Enter your Address',
                          style: TextStyle(
                            fontFamily: 'Montserrat',
                            fontWeight: FontWeight.bold,
                          ),
                        buildCity(city),
                        buildAddress(address),
                        Row(
                          children: [
                            RaisedButton(
                              onPressed: () {
                                if (!_formKey.currentState.validate()) {
                                       return;
                                 }
                                 _formKey.currentState.save(); 

                                print("city - $city"); // prints null
                                print("address- $address"); // prints null
                              },
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ),
    }
}



class _AddressWidgetState extends State<AddressWidgetScreen> {
  
  final myController = TextEditingController();
//a string to store the input from the text field
  String city = '';

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
//in your case TextFormField
        TextField(
  controller: myController,
),
 RaisedButton(
            onPressed: () {
              //store the data from textfield to our new string
              city = myController.text.toString();
      //therest of your logic
            },
   
            textColor: Colors.white,),