颤振更新数据

时间:2020-03-17 16:25:16

标签: flutter dart

我是新手。

更新功能

 void updateProduct(Product product ,String  name, int quantity,String price) {
 _firestore.document(product.date).updateData({
  'name' : name,
  'quantity' :quantity,
  'price' : price,
});
}

产品详细信息页面

    class ProductDetails extends StatefulWidget {
  final Product product;
  ProductDetails(this.product);
  @override
  _ProductDetailsState createState() => _ProductDetailsState();
}

class _ProductDetailsState extends State<ProductDetails> {


final _formProductDetails = GlobalKey<FormState>();

  String name;
  String quantity;
  String price;

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Container(
        color: Colors.grey,
        width: 500,
        height: 500,
        child: Form(
          key: _formProductDetails,
          child: Column(
            children: <Widget>[
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Image.network(
                    widget.product.image,
                    fit: BoxFit.cover,
                  ),
                ],
              ),
              TextFormField(
                initialValue: widget.product.name,
                validator: (val) => val.isEmpty ? '' : null,
                onChanged: (val) =>
                    setState(() => name = val),
              ),
              TextFormField(
                initialValue: widget.product.quantity.toString(),
                validator: (val) => val.isEmpty ? '' : null,
                onChanged: (val) =>
                    setState(() => quantity = val),
              ),
              TextFormField(
                initialValue: widget.product.price,
                validator: (val) => val.isEmpty ? '' : null,
                onChanged: (val) =>
                    setState(() => price = val),
              ),
              RaisedButton(
                onPressed: () async {
                  if (_formProductDetails.currentState.validate()) {
                    ProductService().updateProduct(
                      widget.product,
                      name,
                      int.parse(quantity),
                      price,
                    );
                  }
                },
                child: Text('save'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

当我尝试更改值并更新时,如果更改数量,则更新值。但是,如果单独更新价格或名称,则无法使用。仅更新数量值。我需要分别更新价格,数量和名称。我做错了什么?

0 个答案:

没有答案