简体   繁体   English

在颤振中从 cloud-firestore 检索数据时出错

[英]Error while retrieving data from cloud-firestore in flutter

I have developed a booking app in which I have given an option for promo-code.我开发了一个预订应用程序,其中提供了促销代码选项。 I had created 2 promo codes in the cloud firestore and any users were able to reduce the cost using the promo-code.我在云 Firestore 中创建了 2 个促销代码,任何用户都可以使用促销代码降低成本。 Later I had to create the admin app so in that, I have given an option for the admin for adding a new promo-code from the admin app.the new promo-code is getting stored in the database but I'm not able to use it in my user booking app.can someone help me?后来我不得不创建管理应用程序,因此,我为管理员提供了一个选项,用于从管理应用程序添加新的促销代码。新的促销代码存储在数据库中,但我无法在我的用户预订应用程序中使用它。有人可以帮助我吗?

Here's my code:这是我的代码:

TextEditingController promoController = TextEditingController();

void initState() {
    super.initState();
    promoController.addListener(testListener);
    grandTotal = ticketprice * passengers;
    if (user != null) {
      auth.signOut();
    }
    auth.onAuthStateChanged.listen((u) {
      setState(() => user = u);
    });
  }


 if (promoCode != null && promoCode.isNotEmpty) {
      _firestore
          .collection('promo_codes')
          .where('promo_code', isEqualTo: promoCode)
          .getDocuments()
          .then((QuerySnapshot data) {
        if (data != null && data.documents.length > 0) {
          var documentData = data.documents[0];

          if (documentData['price_reduction'] != null) {
            setState(() {
              total -= documentData['price_reduction'];
            });
          } else if (documentData['percentage_reduction'] != null) {
            setState(() {
              total -= (total * documentData['percentage_reduction']);
            });
          }

          setState(() {
            grandTotal = total;
          });
        }
      });
    } 

  testListener() {
    setState(() {
      promoCode = promoController.text;
    });
  }

I was not able to add my code here, so I have given all the things related to promo-code above please kindly check if someone wants more explanation about the problem I can give them.我无法在这里添加我的代码,所以我已经提供了与上面促销代码相关的所有内容,请检查是否有人想要更多关于我可以给他们的问题的解释。

whenever i had a new promocode from admin app and tires that promocode in my booking app i'm getting the following errors:每当我从管理应用程序中获得新的促销代码和在我的预订应用程序中使用促销代码的轮胎时,我都会收到以下错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'String' is not a subtype of type 'num'
E/flutter (29549): #0      _MyAppState._onChanged.<anonymous closure>.<anonymous closure> (package:fire/main.dart:109:36)
E/flutter (29549): #1      State.setState (package:flutter/src/widgets/framework.dart:1148:30)
E/flutter (29549): #2      _MyAppState._onChanged.<anonymous closure> (package:fire/main.dart:108:13)
E/flutter (29549): #3      _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (29549): #4      _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (29549): #5      _FutureListener.handleValue (dart:async/future_impl.dart:140:18)
E/flutter (29549): #6      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
E/flutter (29549): #7      Future._propagateToListeners (dart:async/future_impl.dart:711:32)
E/flutter (29549): #8      Future._completeWithValue (dart:async/future_impl.dart:526:5)
E/flutter (29549): #9      _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:33:15)
E/flutter (29549): #10     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:291:13)
E/flutter (29549): #11     Query.getDocuments (package:cloud_firestore/src/query.dart)
E/flutter (29549): <asynchronous suspension>
E/flutter (29549): #12     _MyAppState._onChanged (package:fire/main.dart:102:12)
E/flutter (29549): #13     _MyAppState.build.<anonymous closure> (package:fire/main.dart:719:23)
E/flutter (29549): #14     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (29549): #15     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (29549): #16     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (29549): #17     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (29549): #18     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (29549): #19     BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter (29549): #20     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (29549): #21     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter (29549): #22     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (29549): #23     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (29549): #24     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (29549): #25     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (29549): #26     _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (29549): #27     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (29549): #28     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (29549): #29     _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (29549): #30     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)

I think when you fetch data from your firestore, the values documentData['price_reduction'] & documentData['percentage_reduction'] might be in the String datatype.我认为当您从 Firestore 获取数据时,值 documentData['price_reduction'] 和 documentData['percentage_reduction'] 可能在 String 数据类型中。 You are assigning that String value to the "total" value which is int/num data type.您正在将该字符串值分配给 int/num 数据类型的“total”值。

So you should parse that String value to int/num data type.因此,您应该将该字符串值解析为 int/num 数据类型。

if (documentData['price_reduction'] != null) {
            setState(() {
              total -= int.parse(documentData['price_reduction']);
            });
          } else if (documentData['percentage_reduction'] != null) {
            setState(() {
              total -= (total * int.parse(documentData['percentage_reduction']));
            });
          }

Also, make sure values of the documentData['price_reduction'] & documentData['percentage_reduction'] should be number value in String format like "101", "5000", "98056" etc.另外,请确保 documentData['price_reduction'] 和 documentData['percentage_reduction'] 的值应该是字符串格式的数值,如“101”、“5000”、“98056”等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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