【问题标题】:Flutter Firestore error adding value to value from FirestoreFlutter Firestore 错误将值添加到 Firestore 中的值
【发布时间】:2022-11-12 12:07:17
【问题描述】:

我在尝试为 firestore 的值添加值时遇到错误。 我想将 kcal 变量的值与 firestore“进度”值相加

progressAdd = nutr + kcal2;

Error screenshot

Future addMeal() async {
    var nutr;
    await FirebaseFirestore.instance
        .collection('usersData')
        .doc(FirebaseAuth.instance.currentUser!.uid)
        .get()
        .then((doc) => {nutr = doc.data()});
    print('test');
    if (nutr != null) {
      this.progress = nutr['progress'];
      setState(() {
        progressAdd = nutr + kcal2;
        FirebaseFirestore.instance
            .collection('usersData')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .update({'progress': progressAdd});
      });
    } else {
      print('test2');
      return showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              backgroundColor: mainFontColor().tsubColor,
              title: Text(
                ':/',
                textAlign: TextAlign.center,
                style: GoogleFonts.lato(
                    fontWeight: FontWeight.w600, color: Colors.white),
              ),
              content: Container(
                height: 120,
                width: 250,
                child: Column(children: [
                  Text(
                    'Something went wrong!',
                    style: GoogleFonts.dosis(color: Colors.white),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Text(
                    'Go back and try again!',
                    style: GoogleFonts.dosis(color: Colors.white),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Container(
                    height: 40,
                    width: 180,
                    child: ElevatedButton(
                        child: Text('OK'),
                        style: ElevatedButton.styleFrom(
                            backgroundColor: mainFontColor().tsubColor2),
                        onPressed: (() => Navigator.pop(context))),
                  )
                ]),
              ),
            );
          });
    }
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    这一行: .then((doc) => {nutr = doc.data()});

    将类型为Map<String, dynamic>doc.data() 分配给nutr 变量。

    所以这:

     progressAdd = nutr + kcal2;
    

    实际上是无效的,因为您想对无法求和的对象进行加法运算,nutr 的类型为Map<String, dynamic>,而kcal2double

    从我阅读的代码中,我假设您想要获取 Firestore 文档的现有进度并用它的总和 + kcal2 更新它,所以请尝试以下操作:

    代替:

    progressAdd = nutr + kcal2;
    

    替换为:

    progressAdd = this.progress + kcal2;
    

    这现在应该是有效的,您想对 Firestore 中的更新进行汇总。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 2021-01-08
      • 2020-11-14
      • 2021-01-24
      • 2022-11-11
      相关资源
      最近更新 更多