When ‘selectedNumbers’, ‘fRAmount’, ‘sRAmount’ is retrieved from Firestore its not showing correspondently. I attached screenshot how its showing before saved to Firestore and after retrieve. I alos attached screenshot of Firestore.
-
Screenshot Before Save to Firestore on Flutter paymentScreen class – Before saving to firestore screenshot
-
After Retrieve From Firestore on Flutter paymentReponseHanlder class –After retrieve from firestore screenshot
-
Firestore Screenshot – Firestore screenshot
Save to Firestore from Paymenscreen class-
Future<void> createOrderAndInitiatePayment(BuildContext context) async {
List<String?> cleanedFRAmounts = fRAmounts
.where((amount) => amount != null && amount.isNotEmpty)
.toList();
List<String?> cleanedSRAmounts = sRAmounts
.where((amount) => amount != null && amount.isNotEmpty)
.toList();
//order data
final orderData = {
"selected_numbers":
selectedNumbers.map((number) => number.toString()).toList(),
"f_r_amounts": cleanedFRAmounts,
"s_r_amounts": cleanedSRAmounts,
};
// Save to Firestore
try {
await FirebaseFirestore.instance
.collection('orders')
.doc(orderId)
.set(orderData);
print('Order saved to Firestore');
} catch (e) {
print('Error saving order to Firestore: $e');
}
//selectedNumbers, fRAmount, sRAmount saved UI on paymentScreen class
...selectedNumbers.map((number) {
String? frAmount = number < fRAmounts.length
? fRAmounts[number]
: null;
String? srAmount = number < sRAmounts.length
? sRAmounts[number]
: null;
return ListTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
child: Text(
number.toString(), // Convert int to String
style: const TextStyle(
fontSize: 18.0,
),
),
),
),
const SizedBox(width: 16.0),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
child: Text(
(frAmount != null) ? '₹$frAmount' : '×',
style: const TextStyle(
fontSize: 18.0,
),
),
),
),
const SizedBox(width: 16.0),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
child: Text(
(srAmount != null) ? '₹$srAmount' : '×',
style: const TextStyle(
fontSize: 18.0,
),
),
),
),
],
),
);
}),
After Retrieve on payementResponseHandler –
final selectedNumbers = List<int>.from(
(data['selected_numbers'] as List)
.map((e) => int.parse(e.toString())));
final fRAmounts = List<String?>.from(data['f_r_amounts']);
final sRAmounts = List<String?>.from(data['s_r_amounts']);
// UI of selectedNumebers, framount, sramount on paymentresponsehandler
...selectedNumbers.map((number) {
int index = selectedNumbers.indexOf(number);
String? frAmount = index < fRAmounts.length
? fRAmounts[index]
: null;
String? srAmount = index < sRAmounts.length
? sRAmounts[index]
: null;
return ListTile(
title: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
child: Text(
number.toString(),
style: const TextStyle(fontSize: 18.0),
),
),
),
const SizedBox(width: 16.0),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
child: Text(
(frAmount != null) ? '₹$frAmount' : '×',
style: const TextStyle(fontSize: 18.0),
),
),
),
const SizedBox(width: 16.0),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
child: Text(
(srAmount != null) ? '₹$srAmount' : '×',
style: const TextStyle(fontSize: 18.0),
),
),
),
],
),
);
}),
Data should be show same as it is, before saving and after retrieve.
Swarup Nama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.