简体   繁体   中英

How to store integers to Firestore with Flutter

I'm trying to store multiple datatypes in Firebase including Int datatype but when storing Integers to Firebase it doesn't store the value that I set it using SetState function.

I've tried to use If-Statement before storing the value to Database,Nothing changed.

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

Store like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM