如何将输入值汇总或求和到Flutter中的现有字段

时间:2019-09-19 11:16:03

标签: firebase flutter google-cloud-firestore

我想将在文本字段中输入的bool值添加到字段中的现有值上,例如,如果bagWeight = 1,然后输入5,bagWeight应该更新为6。

提供的sharkSpecies相同,SharkSpecies和bagWeight是两个字段

我尝试使用更新,但这会创建一个我不需要的新文档ID或一个新实例

CRUD.DART

import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

class crudMedthods {
  bool isLoggedIn() {
    if (FirebaseAuth.instance.currentUser() != null) {
      return true;
    } else {
      return false;
    }
  }

  Future<void> addData(Data) async {
    if (isLoggedIn()) {

      //carData is
      Firestore.instance.collection('testcrud').document('Kd7RLkMVtu06BDDrxw2j').updateData(Data).catchError((e) {
        print(e);
      });
    } else {
      print('You need to be logged in');
    }
  }
}'

DASHBOARD.DART


  import 'dart:async';

import 'package:flutter/material.dart';

import 'package:cloud_firestore/cloud_firestore.dart';

import 'services/crud.dart';

class DashboardPage extends StatefulWidget {
  @override
  _DashboardPageState createState() => _DashboardPageState();
}

class _DashboardPageState extends State<DashboardPage> {
  String sharkSpecies;
  double bagWeight;
  double count =0.0;

  crudMedthods crudObj = new crudMedthods();

  Future<bool> addDialog(BuildContext context) async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Add Data', style: TextStyle(fontSize: 15.0)),
            content: Column(
              children: <Widget>[
                TextField(
                  decoration: InputDecoration(hintText: 'Enter the shark species'),
                  onChanged: (value) {
                    this.sharkSpecies = value;
                  },
                ),
                SizedBox(height: 5.0),
                TextField(
                  decoration: InputDecoration(hintText: 'Enter the bags weight'),
                  onChanged: (value) {


                    this.bagWeight= double.parse(value);

                  },
                  keyboardType: TextInputType.number,
                ),
              ],
            ),
            actions: <Widget>[
              FlatButton(
                child: Text('Add'),
                textColor: Colors.blue,
                onPressed: () {
                  Navigator.of(context).pop();
                  crudObj.addData({
                    'sharkSpecies': this.sharkSpecies,
                    'bagWeight': this.bagWeight
                  }).then((result) {
                    dialogTrigger(context);
                  }).catchError((e) {
                    print(e);
                  });
                },
              )
            ],
          );
        });
  }

  Future<bool> dialogTrigger(BuildContext context) async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Job Done', style: TextStyle(fontSize: 15.0)),
            content: Text('Added'),
            actions: <Widget>[
              FlatButton(
                child: Text('Alright'),
                textColor: Colors.blue,
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: Text('Dashboard'),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.add),
              onPressed: () {
                addDialog(context);
              },
            )
          ],
        ),
        body: Center());
  }
}

我想将字段更新为包含原始金额加上新金额的值,如摘要中所述:)

0 个答案:

没有答案