that is my code for making use of low cost please anybody inform me why at all times else situation of invalid promocode is exacted even code in right each in firebase and in textual content controller
import 'package deal:firebase_core/firebase_core.dart';
import 'package deal:flutter/materials.dart';
import 'package deal:firebase_database/firebase_database.dart';
void fundamental() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp( MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget construct(BuildContext context) {
return MaterialApp(
dwelling: PaymentScreen(),
);
}
}
class PaymentScreen extends StatefulWidget {
@override
_PaymentScreenState createState() => _PaymentScreenState();
}
class _PaymentScreenState extends State<PaymentScreen> {
double totalAmount = 1000.0;
double discountPercentage = 0.0;
double discountedAmount = 0.0;
String promoCode = "";
TextEditingController promoCodeController = TextEditingController();
late DatabaseReference promoCodesRef;
@override
void initState() {
tremendous.initState();
promoCodesRef = FirebaseDatabase.occasion
.ref('bazaarbuy')
.youngster('sellers')
.youngster('fLx86nObGTPq7KHx4CEnRpe0dTS2')
.youngster('reductions');
// Fetch and set the preliminary low cost share from the database
_fetchInitialDiscountPercentage();
}
Future<void> _fetchInitialDiscountPercentage() async {
DataSnapshot snapshot = (await promoCodesRef.as soon as()).snapshot;
print('Snapshot worth: ${snapshot.worth}'); // Add this line for debugging
if (snapshot.worth != null && snapshot.worth is Map<String, dynamic>) {
Map<String, dynamic> promoCodeData = snapshot.worth as Map<String, dynamic>;
print('Promo code knowledge: $promoCodeData'); // Add this line for debugging
double share = promoCodeData['percentage'];
setState(() {
discountPercentage = share;
print('that is low cost $discountPercentage');
print('that is discount2 $share');
});
}
}
void applyPromoCode() async {
print('Earlier than applyPromoCode');
String enteredPromoCode = promoCodeController.textual content.trim().toLowerCase(); // Convert entered code to lowercase
print('Entered Promo Code (Trimmed): "$enteredPromoCode"');
print('Entered Promo Code Size: ${enteredPromoCode.size}');
DataSnapshot snapshot = (await promoCodesRef.as soon as()).snapshot;
print('Snapshot worth: ${snapshot.worth}');
if (snapshot.worth != null && snapshot.worth is Map<dynamic, dynamic>) {
Map<dynamic, dynamic> promoCodesData = snapshot.worth as Map<dynamic, dynamic>;
print('PromoCodes Information: $promoCodesData');
print('Earlier than promoCodesData.containsKey: $enteredPromoCode');
// Convert keys to lowercase and verify for entered promo code
bool isEnteredCodeValid = promoCodesData.keys.any(
(key) => key.toString().toLowerCase() == enteredPromoCode,
);
if (isEnteredCodeValid) {
print('Entered Promo Code Discovered: $enteredPromoCode');
Map<dynamic, dynamic> promoCodeData = promoCodesData[enteredPromoCode];
print('After: $enteredPromoCode');
String code = promoCodeData['code'];
double share = promoCodeData['percentage'];
String validityDate = promoCodeData['validityDate'];
// Briefly take away validity date verify
bool isCodeValid = true;
print('Promo Code Particulars:');
print('Code: $code');
print('Proportion: $share');
print('Validity Date: $validityDate');
print('Is Code Legitimate: $isCodeValid');
if (isCodeValid) {
setState(() {
discountPercentage = share;
promoCode = code;
// Calculate the discounted whole quantity
discountedAmount = totalAmount - (totalAmount * (discountPercentage / 100));
// Debugging print statements
print('Discounted Quantity: $discountedAmount');
print('Entered Promo Code: $enteredPromoCode');
print('Promo Code Particulars:');
print('Code: $code');
print('Proportion: $share');
print('Validity Date: $validityDate');
print('Is Code Legitimate: $isCodeValid');
});
} else {
// Promo code has expired
setState(() {
discountPercentage = 0.0;
promoCode = "";
// Reset the discounted quantity to the unique whole quantity
discountedAmount = totalAmount;
// Debugging print statements
print('Promo Code Expired');
});
}
} else {
// Promo code not discovered
setState(() {
discountPercentage = 0.0;
promoCode = "";
// Reset the discounted quantity to the unique whole quantity
discountedAmount = totalAmount;
// Debugging print statements
print('Invalid Promo Code');
});
}
} else {
// Deal with the case when snapshot.worth will not be a Map
print('Invalid Snapshot Information');
}
print('After applyPromoCode');
}
bool _isValidDate(String validityDate) {
DateTime currentDate = DateTime.now();
DateTime promoDate = DateTime.parse(validityDate);
print('Present Date: $currentDate');
print('Promo Date: $promoDate');
return promoDate.isAfter(currentDate);
}
@override
Widget construct(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Textual content('Fee Display screen'),
),
physique: Middle(
youngster: Column(
mainAxisAlignment: MainAxisAlignment.heart,
youngsters: <Widget>[
Text('Original Price: $${totalAmount.toStringAsFixed(2)}'),
Text('Applied Promo Code: $promoCode'),
Text('Discount Percentage: ${discountPercentage.toStringAsFixed(2)}%'),
if (discountedAmount > 0)
Text('Discounted Price: $${discountedAmount.toStringAsFixed(2)}'),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 200,
child: TextField(
controller: promoCodeController,
decoration: InputDecoration(
labelText: 'Enter Promo Code',
),
),
),
ElevatedButton(
onPressed: applyPromoCode,
child: Text('Apply Promo Code'),
),
],
),
],
),
),
);
}
}
that is my code for making use of low cost please anybody inform me why at all times else situation of invalid promocode is exacted even code in right each in firebase and in textual content controller