Unable to upload data to firebase realtime database from my flutter application

Viewed 21

I am trying to add a module in my flutter project where users will be able to upload their addresses from. I'm using a http package for this. On saving the data, however, only an empty string is being uploaded onto the database. Here's my code:

This is the validator code from the text form editing field:

 validator: (value) {
                    if (value!.isEmpty) {
                      return 'Please enter a description.';
                    }
                    if (value.length < 10) {
                      return 'Should be at least 10 characters long.';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    addressEditingController.text = value!;
                    _editedAddress = Address(address: value);
                  },
),
              ),
              IconButton(onPressed:(){
                if(_form.currentState!.validate()){
                uploadAddress();

Here is the uploadAddress() function:

void uploadAddress() {
    
    setState(() {
      
      _editedAddress = Address(
          address: _editedAddress.address);
    });
    Provider.of<addAddress>(context, listen: false)
        .addProduct(_editedAddress)
        .catchError((error) {
      return showDialog(
          context: context,
          builder: (ctx) => AlertDialog(
                title: Text("An error occurred"),
                content: Text(error.toString()),
                actions: <Widget>[
                  TextButton(
                      onPressed: () {
                        Navigator.of(ctx).pop();
                      },
                      child: Text("Okay"))
                ],
              ));
    }).then((_) {
      setState(() {
        _isLoading = false;
      });
      
    });
  }

Here's the addProduct() function:

Future<void> addProduct(Address address) {
    final url = Uri.https(
        'internship-75b58-default-rtdb.firebaseio.com', '/addresses/${user!.phoneNumber}.json');
    return http
        .post(url,
            body: json.encode({
              'address':address.address,
            }))
        .then((response) {
      final newProduct = Address(
          address: address.address,);
      

      notifyListeners();
    }).catchError((error) {
      print(error);
      throw error;
    });
  }

Any help would be greatly appreciated:)

0 Answers
Related