问题
I have the following function which extracts order data from Db for each user. My goal is to print each user's orders. However, I am getting some weird errors.
Here's the function:
Future<void> fetchAllOrders() async {
final url =
'https://tb-936.firebaseio.com/orders.json?auth=$authToken';
final response = await http.get(url);
final List<OrderItem> loadedOrders = [];
final extractedData = json.decode(response.body) as Map<String, dynamic>;
print('Extracted data is ' + extractedData.toString());
extractedData.forEach((userId, orderData) {
print('Order data is ' + orderData.toString());
print(orderData['amount']);
print(orderData['dateTime']);
print(orderData['deliveryMethod']);
print(orderData['uniqueOrderNumber']);
print(orderData['isOrderComplete']);
print(orderData['userId']);
});
}
Here's console log for the extracted data:
{12345: {-MMqL1Tkg-XXNHbKGz62: {amount: 75000.0, dateTime: 2020-11-23T21:22:51.817939, deliveryMethod: delivery, isOrderComplete: false, products: [{id: 2020-11-23 21:22:47.023541, price: 25000.0, quantity: 3, title: Mchuzi}], uniqueOrderNumber: TBOJ43, userId: 12345}}, 67890: {-MNY9DoItO4Vo9K0EOd5: {amount: 250000.0, dateTime: 2020-12-02T14:14:12.022284, deliveryMethod: pickup, isOrderComplete: false, products: [{id: 2020-12-02 14:13:32.093068, price: 25000.0, quantity: 10, title: Mchuzi}], uniqueOrderNumber: TBQKJP, userId: 67890}}}
Where 12345 and 67890 are user Ids.
Here's the console log for the orderData inside the for loop:
Order data is {-MMqL1Tkg-XXNHbKGz62: {amount: 75000.0, dateTime: 2020-11-23T21:22:51.817939, deliveryMethod: delivery, isOrderComplete: false, products: [{id: 2020-11-23 21:22:47.023541, price: 25000.0, quantity: 3, title: Mchuzi}], uniqueOrderNumber: TBOJ43, userId: 12345}}
I/flutter ( 5975): null
I/chatty ( 5975): uid=10227(com.example.tb) 1.ui identical 4 lines
I/flutter ( 5975): null
I/flutter ( 5975): Order data is {-MNY9DoItO4Vo9K0EOd5: {amount: 250000.0, dateTime: 2020-12-02T14:14:12.022284, deliveryMethod: pickup, isOrderComplete: false, products: [{id: 2020-12-02 14:13:32.093068, price: 25000.0, quantity: 10, title: Mchuzi}], uniqueOrderNumber: TBQKJP, userId: 67890}}
I/flutter ( 5975): null
I/chatty ( 5975): uid=10227(com.example.tb) 1.ui identical 4 lines
I/flutter ( 5975): null
D/Surface ( 5975): Surface::disconnect(this=0x7e78146000,api=1)
D/Surface ( 5975): Surface::disconnect(this=0x7e78146000,api=-1)
D/Surface ( 5975): Surface::disconnect(this=0x7e78033000,api=1)
Any ideas why I am getting nulls?
回答1:
As mentioned by @Adithya Shetty, you are using the wrong keys because your orderData is actually an object that has only one property - {-MMqL1Tkg-XXNHbKGz62 for user 12345 and -MNY9DoItO4Vo9K0EOd5 for user 67890.
To be able to achieve the result you need and access it dynamically, you need to add another forEach inside your forEach:
Future<void> fetchAllOrders() async {
final url =
'https://tb-936.firebaseio.com/orders.json?auth=$authToken';
final response = await http.get(url);
final List<OrderItem> loadedOrders = [];
final extractedData = json.decode(response.body) as Map<String, dynamic>;
print('Extracted data is ' + extractedData.toString());
extractedData.forEach((userId, order) {
print('User id: $userId');
order.forEach((orderKey, orderData) {
print('Order data is ' + orderData.toString());
print('Order key: $orderKey');
print(orderData['amount']);
print(orderData['dateTime']);
print(orderData['deliveryMethod']);
print(orderData['uniqueOrderNumber']);
print(orderData['isOrderComplete']);
print(orderData['userId']);
});
});
}
回答2:
You are using the wrong keys that is why you are getting null.
To get amount from the nested map, you will have to do something like this:
map data1 = orderData['12345'];
map data2 = data1['-MMqL1Tkg-XXNHbKGz62'];
map amount = data2['amount'];
From the orderData.toString output you can get the idea about the map structure.
Your output is as follows:
{12345: {-MMqL1Tkg-XXNHbKGz62: {amount: 75000.0, dateTime: 2020-11-23T21:22:51.817939, deliveryMethod: delivery, isOrderComplete: false, products: [{id: 2020-11-23 21:22:47.023541, price: 25000.0, quantity: 3, title: Mchuzi}], uniqueOrderNumber: TBOJ43, userId: 12345}}, 67890: {-MNY9DoItO4Vo9K0EOd5: {amount: 250000.0, dateTime: 2020-12-02T14:14:12.022284, deliveryMethod: pickup, isOrderComplete: false, products: [{id: 2020-12-02 14:13:32.093068, price: 25000.0, quantity: 10, title: Mchuzi}], uniqueOrderNumber: TBQKJP, userId: 67890}}}
Now if you look care fully you will find 12345 and 67890 as the first key's inside this complex maps, which is the `userID'
now,
the key 12345 contains the nested map with keys and data as follows:
-MMqL1Tkg-XXNHbKGz62: {amount: 75000.0, dateTime: 2020-11-23T21:22:51.817939, deliveryMethod: delivery, isOrderComplete: false, products: [{id: 2020-11-23 21:22:47.023541, price: 25000.0, quantity: 3, title: Mchuzi}], uniqueOrderNumber: TBOJ43, userId: 12345}
-MMqL1Tkg-XXNHbKGz62 is a unique key and the data you want is mapped inside it
来源:https://stackoverflow.com/questions/65131521/extracting-data-from-database-in-dart