繁体   English   中英

尝试更新 flutter 中的数据时出错 firebase - 未处理的异常:无效参数:“TextEditingController”实例

[英]Getting error while trying to update the data in flutter firebase - Unhandled Exception: Invalid argument: Instance of 'TextEditingController'

我正在尝试更新数据。 我通过传递它从另一个页面获取值,并且数据显示在文本字段中。 但是当我尝试更新数据时,数据没有更新。 谁能告诉我我在这里做错了什么。 我收到一个错误,作为Unhandled Exception: Invalid argument: Instance of 'TextEditingController

这是我的完整编码

class updateAddress extends StatefulWidget {
final String firstName;
final String lastName;
final String region;
final String city;
final String address1;
final String address2;
final String primaryPhone;
final String secondaryPhone;

const updateAddress(
  {Key? key,
  required this.firstName,
  required this.lastName,
  required this.region,
  required this.city,
  required this.address1,
  required this.address2,
  required this.primaryPhone,
  required this.secondaryPhone})
  : super(key: key);

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

class _updateAddressState extends State<updateAddress> {
late TextEditingController FirstName =
  TextEditingController(text: "${widget.firstName}");
late TextEditingController LastName =
  TextEditingController(text: "${widget.lastName}");
late TextEditingController Region =
  TextEditingController(text: "${widget.region}");
late TextEditingController City =
  TextEditingController(text: "${widget.city}");
late TextEditingController Address1 =
  TextEditingController(text: "${widget.address1}");
late TextEditingController Address2 =
  TextEditingController(text: "${widget.address2}");
 late TextEditingController PrimaryPhone =
  TextEditingController(text: "${widget.primaryPhone}");
late TextEditingController SecondaryPhone =
  TextEditingController(text: "${widget.secondaryPhone}");

@override
Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.grey.shade100,
  appBar: AppBar(
    centerTitle: true,
    backgroundColor: Theme.of(context).scaffoldBackgroundColor,
    title: Text(
      'Update Address',
      style: TextStyle(color: Colors.black),
    ),
    leading: IconButton(
      icon: Icon(
        Icons.arrow_back_ios,
        color: Colors.black,
      ),
      onPressed: () {
        Navigator.of(context).pushNamed('/addressProfile');
      },
    ),
  ),
  body: Card(
    margin: EdgeInsets.all(20),
    child: Padding(
      padding: EdgeInsets.all(15),
      child: GestureDetector(
        onTap: () {
          FocusScope.of(context).unfocus();
        },
        child: ListView(
          children: [
            SizedBox(
              height: 30,
            ),
            TextFormField(
              maxLength: 20,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: FirstName,
              decoration: InputDecoration(
                labelText: "First Name",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            TextFormField(
              maxLength: 20,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: LastName,
              decoration: InputDecoration(
                labelText: "Last Name",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            TextFormField(
              maxLength: 15,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: Region,
              decoration: InputDecoration(
                labelText: "Region",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            TextFormField(
              maxLength: 15,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: City,
              decoration: InputDecoration(
                labelText: "City",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            TextFormField(
              maxLength: 20,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: Address1,
              decoration: InputDecoration(
                labelText: "Address Line 1",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            TextFormField(
              maxLength: 20,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: Address2,
              decoration: InputDecoration(
                labelText: "Address Line 2",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            TextFormField(
              keyboardType: TextInputType.phone,
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly,
              ],
              maxLength: 10,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: PrimaryPhone,
              decoration: InputDecoration(
                labelText: "Primary Phone",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            TextFormField(
              keyboardType: TextInputType.phone,
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly,
              ],
              maxLength: 10,
              style: TextStyle(color: Colors.black, fontSize: 20),
              controller: SecondaryPhone,
              decoration: InputDecoration(
                labelText: "Secondary Phone",
                floatingLabelBehavior: FloatingLabelBehavior.always,
              ),
            ),
            SizedBox(
              height: 30,
            ),
            Align(
              child: ElevatedButton(
                onPressed: () {
                  FirebaseFirestore.instance
                      .collection('address')
                      .doc()
                      .update({
                    "firstName": FirstName,
                    "lastName": LastName,
                    "region": Region,
                    "city": City,
                    "address1": Address1,
                    "address2": Address2,
                    "primaryPhone": PrimaryPhone,
                    "secondaryPhone": SecondaryPhone
                  });
                  Fluttertoast.showToast(
                    msg: "Address updated Successfully",
                    toastLength: Toast.LENGTH_SHORT,
                    gravity: ToastGravity.BOTTOM,
                    textColor: Colors.black,
                    backgroundColor: Colors.green.shade400,
                  );
                },
                child: Text(
                  "Update Address",
                  style: TextStyle(
                      fontSize: 15, letterSpacing: 2, color: Colors.black),
                ),
                style: ElevatedButton.styleFrom(
                    fixedSize: Size(250, 40),
                    primary: Colors.green.shade500,
                    padding: EdgeInsets.symmetric(
                      horizontal: 50,
                    ),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10))),
              ),
            )
          ],
        ),
      ),
    ),
  ),
);
}
}

我需要在 textFormfild 中传递数据并查看用户输入的内容,然后更新用户输入。 但是在这里用户输入没有更新。 我在 EditTextController 中显示传递值 ${widget.value} 和初始值。

我已经尝试添加为 firstname.text

但这并不能解决问题。 它显示相同的错误并且Some requested document was not found错误。 但是该文档在数据库中可用。 我也在 the.doc() 中添加了 documentid。 但没有变化

您当前传递的是TextEditingController的 object 而不是String形式的text

要从TextEditingController获取文本,请使用text属性,如下所示:

 FirebaseFirestore.instance
      .collection('address')
      .doc()
      .update({
      "firstName": FirstName.text,
      "lastName": LastName.text,
      "region": Region.text,
      "city": City.text,
      "address1": Address1.text,
      "address2": Address2.text,
      "primaryPhone": PrimaryPhone.text,
      "secondaryPhone": SecondaryPhone.text
});

在 firebase 更新 function 中,您传递的是TextEditingController ,而不是String

代替:

FirebaseFirestore.instance
   .collection('address')
   .doc()
   .update({
      "firstName": FirstName,
      "lastName": LastName,
      "region": Region,
      "city": City,
      "address1": Address1,
      "address2": Address2,
      "primaryPhone": PrimaryPhone,
      "secondaryPhone": SecondaryPhone
   });

和:

FirebaseFirestore.instance
   .collection('address')
   .doc()
   .update({
      "firstName": FirstName.text,
      "lastName": LastName.text,
      "region": Region.text,
      "city": City.text,
      "address1": Address1.text,
      "address2": Address2.text,
      "primaryPhone": PrimaryPhone.text,
      "secondaryPhone": SecondaryPhone.text
   });

查看TextEditingController文档以获取更多详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM