在Flutter中从Cloud-Firestore检索数据时出错

时间:2019-12-15 11:20:49

标签: firebase flutter dart google-cloud-firestore e

我开发了一个预订应用程序,在其中我给出了促销代码选项。我在Cloud Firestore中创建了2个促销代码,任何用户都可以使用促销代码来降低成本。后来我不得不创建管理应用程序,因此我给了管理员一个选项,可以从管理应用程序中添加新的促销代码。新的促销代码已存储在数据库中,但无法在我的用户预订应用程序中使用。有人可以帮我吗?

这是我的代码:

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

我无法在此处添加我的代码,所以我已经提供了与以上promocode相关的所有信息,请检查一下,如果有人想进一步解释我可以给他们的问题

每当我从管理应用中获得新的促销代码并在预订应用中进行促销时,都会出现以下错误:

[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)

1 个答案:

答案 0 :(得分:0)

我认为,当您从Firestore中获取数据时,值documentData ['price_reduction']和documentData ['percentage_reduction']可能位于String数据类型中。您正在将该字符串值分配给int / num数据类型的“总计”值。

因此,您应该将该String值解析为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']));
            });
          }

还要确保documentData ['price_reduction']和documentData ['percentage_reduction']的值应为字符串格式的数字值,例如“ 101”,“ 5000”,“ 98056”等。