-1

嗨,我有一个地址页面,我在其中获取用户的详细信息。有一个表单还提供了所有必要的验证我想获取用户提供的所有详细信息并将其存储在列表中,以便我可以使用该列表并将其存储在数据库中。但是,每当我尝试将这些元素添加到列表中并打印相同的元素时,列表都会显示为空。

这是我的代码。

  TextFormField(
              decoration: InputDecoration(

                  hintText: 'Enter your Name'),
              maxLength: 20,
              validator: (String value) {
                address.clear();
                if (value.isEmpty) {
                  return 'Name is Required';
                }

                return null;
              },

              onSaved: (String value) {
                _name = value;
                address.add(value);



              },
          ),
RaisedButton(
              color: Colors.red,
              child: Text('Proceed to order',style: TextStyle(color: Colors.white),),
              onPressed: (){
                if(_formKey.currentState.validate()) {

                  print("address" + address.toString());
                  switch(widget.font) {

                    case'a': {

                      Navigator.push(context, MaterialPageRoute(builder: (context)=>Onlinepage()));
                      /*Navigator.push(context, MaterialPageRoute(builder: (context)=>Onlinepage()));*/

                    }
                    break;

                    case 'b': {

                      Navigator.push(context, MaterialPageRoute(builder: (context)=>Codpage(address:address,orderListFinal:widget.orderListFinal,total:widget.total)));



                    }
                    break;

这是输出 I/flutter (19488): address[] 下面是整个代码

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:kartofill/Homepagewithprice/Codpage.dart';
import 'package:kartofill/Homepagewithprice/Onlinepage.dart';


var address = [];

class Deliveryaddresspage extends StatefulWidget {

  var total;
  List orderListFinal = [];
  var font;



  Deliveryaddresspage({this.font, this.orderListFinal, this.total});

  @override
  _DeliveryaddresspageState createState() => _DeliveryaddresspageState();
}

class _DeliveryaddresspageState extends State<Deliveryaddresspage> {
  String _name;
  String _pincode;
  String _address1;
  String _address2;
  String _address3;
  String _phoneNumber;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Row(
        children: <Widget>[
          Expanded(

            child: RaisedButton(
              color: Colors.red,
              child: Text('Proceed to order',style: TextStyle(color: Colors.white),),
              onPressed: (){
                if(_formKey.currentState.validate()) {

                  print("address" + address.toString());
                  switch(widget.font) {

                    case'a': {

                      Navigator.push(context, MaterialPageRoute(builder: (context)=>Onlinepage()));
                      /*Navigator.push(context, MaterialPageRoute(builder: (context)=>Onlinepage()));*/

                    }
                    break;

                    case 'b': {

                      Navigator.push(context, MaterialPageRoute(builder: (context)=>Codpage(address:address,orderListFinal:widget.orderListFinal,total:widget.total)));



                    }
                    break;



                    default: {
                      Fluttertoast
                          .showToast(
                          msg: "Please Select any one of the above options",
                          toastLength: Toast
                              .LENGTH_SHORT,
                          gravity: ToastGravity
                              .CENTER,
                          timeInSecForIosWeb: 1,
                          backgroundColor: Colors
                              .red,
                          textColor: Colors
                              .white,
                          fontSize: 16.0
                      );
                    }
                    break;
                  }
                }


              },

            ),
          )
        ],

      ),
      appBar: AppBar(
        title: Text('KartOfill'),
        backgroundColor: Colors.red,
      ),
      body: SingleChildScrollView(
        child: Container(
          width: MediaQuery.of(context).size.width,
          child: Form(
            key: _formKey,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: <Widget>[

                  Center(child: Text('Delivery Address',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 25),)),
                  SizedBox(height: MediaQuery.of(context).size.height*.001,),
          TextFormField(
              decoration: InputDecoration(

                  hintText: 'Enter your Name'),
              maxLength: 20,
              validator: (String value) {
                address.clear();
                if (value.isEmpty) {
                  return 'Name is Required';
                }

                return null;
              },

              onSaved: (String value) {
                _name = value;
                address.add(value);



              },
          ),

                  TextFormField(
                    decoration: InputDecoration(hintText: 'Enter 10 digit phone Number'),
                    maxLength: 10,
                    keyboardType: TextInputType.number,
                    inputFormatters: [
                      new BlacklistingTextInputFormatter(
                          new RegExp(
                              '[\\.|\\,|\\-|\\ ]'))
                    ],
                    validator: (String value) {
                      if (value.isEmpty) {
                        return 'Phone number is Required';
                      }

                      if( value.length < 10 || !RegExp(r'^[0-9]+$').hasMatch(value))
                      {

                        return 'Please enter correct 10 digit phone number';
                      }

                      return null;
                    },
                    onSaved: (String value) {
                      _phoneNumber = value;

                    },
                  ),
                  TextFormField(
                    decoration: InputDecoration(hintText: 'Address 1'),
                    maxLength: 20,
                    validator: (String value) {
                      if (value.isEmpty) {
                        return 'Address is Required';
                      }

                      return null;
                    },
                    onSaved: (String value) {
                      _address1 = value;


                    },
                  ),
                  TextFormField(
                    decoration: InputDecoration(hintText: 'Address 2'),
                    maxLength: 20,

                    onSaved: (String value) {
                      _address2 = value;

                    },
                  ),
                  TextFormField(
                    decoration: InputDecoration(hintText: 'Address 3'),
                    maxLength: 20,

                    onSaved: (String value) {
                      _address3 = value;

                    },
                  ),
                  TextFormField(
                    decoration: InputDecoration(labelText: 'Pincode'),
                    maxLength: 6,
                    keyboardType: TextInputType.number,
                    inputFormatters: [
                      new BlacklistingTextInputFormatter(
                          new RegExp(
                              '[\\.|\\,|\\-|\\ ]'))
                    ],
                    validator: (String value) {
                      if (value.isEmpty  ) {
                        return 'Pincode is Required';
                      }
                      else if (value.length < 6 || !RegExp(r'^[0-9]+$').hasMatch(value)){

                        return 'Please enter correct pincode';
                      }

                      return null;
                    },
                    onSaved: (String value) {
                      _pincode = value;

                    },
                  ),



                ],
              ),
            ),

          ),

        ),
      ),
    );
  }
}


4

1 回答 1

0

尝试使用onChanged:(){}而不是使用 onSaved

于 2020-09-06T11:19:23.953 回答