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. 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. You are assigning that String value to the "total" value which is int/num data type.
So you should parse that String value to int/num data type.
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.
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.