Unable to add new product

Issue

I still get update new product but when I add a new product nothing happen so what’s the problem, pls help me. Thanks

void _saveForm() {
    final isValid = _form.currentState!.validate();
    if (!isValid) {
      return;
    }
    _form.currentState!.save();
    if (_editedProduct.id != null) {
      Provider.of<Products>(context, listen: false)
          .updateProduct(_editedProduct.id, _editedProduct);
    } else {
      Provider.of<Products>(context, listen: false).addProduct(_editedProduct);
    }
    Navigator.of(context).pop();
  }

Solution

First assign an empty String to the ‘id’ argument of _editedProduct, then change this condition "if (_editedProduct.id != null)" to this "if (_editedProduct.id.isNotEmpty)".

You should be able to add a new product after this.

Answered By – Reazy_ai

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published