Home > Software engineering >  Getting error while trying to update the data in flutter firebase - Unhandled Exception: Invalid arg
Getting error while trying to update the data in flutter firebase - Unhandled Exception: Invalid arg

Time:10-10

I am trying to update a data. I got the values form another page by passing it, and the data is showing in the text field. But When I try to update the data is not updating. Can anyone tell what I am doing wrong here. I am getting an error as Unhandled Exception: Invalid argument: Instance of 'TextEditingController

This is my complete coding

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))),
              ),
            )
          ],
        ),
      ),
    ),
  ),
);
}
}

I need to pass the data in the textFormfild and view what the user have typed and then update user input. But In here user input is not updating. I am showing the passed value ${widget.value} in the EditTextController and an initial value.

I already tried adding as firstname.text

But that doesn't resolve the issue. Its showing the same error and Some requested document was not found error too. But The document is availavle in the database. I have added the documentid in the .doc() too. But no changes

CodePudding user response:

You're currently passing an object of TextEditingController instead of the text in String form.

To get the text from a TextEditingController use the text property like so:

 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
});

CodePudding user response:

In the firebase update function, you are passing the TextEditingController, instead of String.

Replace:

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

With:

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
   });

Check the TextEditingController documentation for more details

  • Related