如何使用Flutter将整数存储到Firestore

时间:2019-08-30 18:56:05

标签: android ios firebase flutter google-cloud-firestore

我试图在Firebase中存储多个数据类型,包括Int数据类型,但是在将Integers存储到Firebase时,它不存储我使用SetState函数设置的值。

在将值存储到数据库之前,我尝试使用If-Statement,什么都没有改变。

int itemBarcode;
int itemBonusQTY=0;
int itemCostPrice=0;
Future<void> addData(productsData) async {
  Firestore.instance.collection('Products').add(productsData).catchError((e) 
{
print(e);
});}

Future toDataBase(BuildContext context) async {
 addData({
    'ItemBarcode': this.itemBarcode,
    'BonusQTY': this.itemBonusQTY,
    'CostPrice': this.itemCostPrice
 }).then((result) {
 }).catchError((e) {
    print(e);
 });
}
Future addDialog(BuildContext context)  async {
await showDialog(

TextField(
decoration: InputDecoration(
   onChanged: (value) {
    this.itemBarcode = value as int;
},

TextField(
 decoration: InputDecoration(
   onChanged: (value) {
   if (itemBonusQTY != null) {
      this.itemBonusQTY = value as int;
 } else {
   setState(() {
    itemBonusQTY = 0;
 });}
 },)

TextField(
 decoration: InputDecoration(
   onChanged: (value) {
  if (itemCostPrice!= null) {
     this.itemCostPrice= value as int;
 } else {
   setState(() {
    itemCostPrice= 0;
 });}
 },)
FlatButton(
 child: Text('Add'),
 textColor: Colors.amber,
 onPressed: () {
   toDataBase(context);
   Navigator.of(context).pop();
},
 ),

Output in DB 
    itemBarcode = null
    itemBonusQTY = 0
    itemCostPrice = 0

1 个答案:

答案 0 :(得分:2)

像这样存储

Future toDataBase(BuildContext context) async {
 addData({
    'ItemBarcode': int.parse(this.itemBarcode),
    'BonusQTY': int.parse(this.itemBonusQTY),
    'CostPrice': int.parse(this.itemCostPrice)
 }).then((result) {
 }).catchError((e) {
    print(e);
 });
}