forEach not working in my map iteration. Any experts here? - flutter

I'm working with an app in Flutter that writes data to a Firebase database that I created and then reads and displays it in a list view in my app. Currently the app writes the data correctly and also reads it and prints it to the command line. However, when I code the bit that's supposed to then take that data and dump it into a list that can be printed in list view I get the following error when trying to iterate it from a map to a list using a forEach command. The error is "The method 'forEach' can't be unconditionally invoked because the receiver can be 'null'."
I've even tried adding '!' to force it to run since I know it's not null but it still won't work. Any ideas? The code for that section is below. Thanks in advance!
_ListViewReadPageState() {
// load all students from firebase database and display them in list view
FirebaseDatabase.instance.ref("students").once()
.then((databaseEvent) {
print("Successfully loaded the data");
print(databaseEvent);
print("Key:");
print(databaseEvent.snapshot.key);
print("value:");
print(databaseEvent.snapshot.value);
print("Iterating the value map");
var studentTmpList = [];
databaseEvent.snapshot.value.forEach((k, v) {
print(k);
print(v);
studentTmpList.add(v);
});
print("Final student list");
print(studentTmpList);
studentList = studentTmpList;
setState(() {
});
}).catchError((error) {
print("Failed to load the data");
print(error);
});
}
I've tried adding '!' to force the code to run since I know the data isn't null but no dice. When I try adding the '!' I get a new error stating "The method 'forEach' isn't defined for the type 'Object'".

Related

How to read data from cloud firestore with firebase function and loop and push it to list

I am trying to read data from firestore collection to get notification token and push it to a list and pass the list to SendtoDevice function to send push notification.
I am facing issue when using foreach function and push the data getting error "for each is not a valid function"
let tokenList = [];
const userNotificationTokenDocs = await db.collection("userToken").doc(userId).get()
.then(querySnapshot => {
querySnapshot.forEach((doc) => {
console.log(doc.data().Tokens);
tokenList.push(doc.data().Tokens);
});
return null;
});
**other option which i tried, with same error.**
userNotificationTokenDocs.forEach(function(docs){
console.log(docs.data());
if(docs.data() != null){
tokenList.push(docs.data().Token);
}
});
Please help me, I am stuck with this.
found some BASIC mistake in your coding.
const userNotificationTokenDocs will always null , because you return null. forEach is void function.
Using await and then is unecessery. choose 1.
=> arrow is a shorthand for { return expr; } .
if you only have 1 expresion, you can shorthand with arrow. but if you have morethan 1 expression, just use bracket {} . documentation
userNotificationTokenDocs.forEach(function(docs){ this will error since null value cant use forEach.
I think your IDE should give you alert there's an error in your code. but you choose to run it instead fixing the error.
but is ok, keep learning code. :)

How to sequentially send requests to the server?

I have a List, and I need to send each element of this List to the server sequentially. And if the answer is with an error, then do not continue sending, but display an error. I am trying to use
await Future.wait([]);
but I do not quite understand how to complete the send on failure, and how to iterate over the elements.
To iterate over elements through .forEach(element) {}
The compiler throws an error: The argument type 'List' can't be assigned to the parameter type 'Iterable<Future>'
Here is what I am trying to do:
await Future.wait([
state.revisionFill.forEach((element) {
var successOrFail = await ContainerExpire.call(ContainerExpireParams(token: token, entity: element.entity));
})
]);
I'm new and don't fully understand async/sync requests in flutter. Tell me which direction to study.
How to complete the next send if it fails?
This seems to be what you're looking for:
for (var element in state.revisionFill) {
var successOrFail = await ContainerExpire.call(ContainerExpireParams(token: token, entity: element.entity));
if (successOrFail.hasFailed()) {
//add code for some possible error handling
break;
}
}
I am assuming that based on the value of successOrFail you can tell whether or not an error occured. Basically after each request you check for the status, and break on an error. You can add logic for displaying your error message.

FireStore read fails silently and I have no idea why

Help is much appreciated how to trace down this issue, because I am running out of ideas.
I am calling the function getOrderCollection, below, but it aborts after the first line var myCompanyDoc = await FirebaseFirestore.instance.collection('companies').doc(myCompany).get(); Without trowing anything to the console or jumping into some library when debugging. When I click next statement it jumps back to the calling function.
I am authenticated to the database, companyCollection = FirebaseFirestore.instance.collection('companies') provides an initialized object pointing to the collection and myCompany is a constant with the document id entered by copy/paste.
If some rules for the database but I can't see successful or denied queries with the monitor.
Any ideas how I can proceed tracing down the issue?
Future<void> getOrderCollection() async {
var myCompanyDoc = await FirebaseFirestore.instance.collection('companies').doc(myCompany).get();
print("companyDoc fetched");
final myDeliveryDocRef = myCompanyDoc.data()['delivery'].toString();
orderCollection = FirebaseFirestore.instance.collection('companies').doc(myCompany).collection('features').doc(myDeliveryDocRef).collection('orders');
orderBriefDoc = FirebaseFirestore.instance.collection('companies').doc(myCompany).collection('features').doc(myDeliveryDocRef);
}
UPDATE: This is collection > document what corresponds to final String myCompany = '4U4kZKXkr3rHA6B04S5K';
As we discussed in your comments, the issue was that you forgot to await the getOrderCollection() function. Even though, as you mentioned, your caller function _deliveryRepository.initRepository() was awaited, you still had to await getOrderCollection() inside your caller method to make sure that the code is waiting for the getOrderCollection() to be executed before it proceeds to the next line.
In general, you want to have some error handling and to type the known types/classes (avoid using var).
Error handling - for async/await place the code inside a try/catch.
Typing - Dart is type safe, which is really great to prevent runtime errors.
Depending on your setup, you might be able to hover over the Firestore.instance.collection(...).doc(...) to see the return type. .doc(...).get() returns a DocumentSnapshot and .collection(...).get() returns a CollectionSnapshot.
Using the above, it should be easier to debug:
Future<void> getOrderCollection() async {
try {
DocumentSnapshot myCompanyDoc = await FirebaseFirestore.instance.collection('companies').doc(myCompany).get();
print("companyDoc fetched");
final myDeliveryDocRef = myCompanyDoc.data()['delivery'].toString();
} catch(e) {
print('Error: ' + e.toString());
}
}
Don't forget to await your other 2 Firestore queries.

The method 'add' was called on null : Flutter, SQFlite

I am getting data from SQLite in flutter. When I try to convert the list of Map to list of Objects. It gives the error of
The method 'add' was called on null
On debuging, it shows that it has data but still gives the error.
Here is my code
List<Image> imagesList;
if (imagesListMap != null) {
imagesListMap.forEach((element) {
imagesList.add(Image.FromDatabase(element));
});
}
And its the debugging screenshot
You need to initialize the List like this before calling add() on it..
List<Image> imagesList = [];

How to handle non explicit errors inside sails.js helpers?

I am trying to figure out how the Error handling in Sails.js works. Unfortunatley the code examples in the docs do not cover this use case.
The problem is I keep getting this error:
UsageError: `.intercept()` handler returned `undefined`, but this should never happen.
Regardless, here is a summary of the original underlying error:
Now all I am trying to do is call a helper and if it fails, then I want to catch the error (any), log it and run some code. If I wouldn't be using Sails but normal promises I would have handled it like this:
await helper().catch((err) => { // run some code }
In Sails I should be able to use .intercept() instead of .catch()
My code looks like this:
// ExportController.js
const csv = await sails.helpers.files.convertToCsv(data)
.intercept((err) => {
sails.log.error(err)
req.addFlash('error_messages', 'Error parsing data to csv!')
return res.redirect(`/`);
})
// convert-to-csv.js
if (!Array.isArray(inputs.data)) {
throw new Error('invalid inputs.data type: ' + typeof inputs.data)
};
Now how can I avoid getting this error?
The code examples show only cases where errors that are explicitly added to the exits object are handled, but not for general error handling.
In the docs it says that if the filter argument is
not provided, ALL errors will be intercepted.
Or is that only true for db queries? Because the .intercept() doc section is in that subcategory.
You could use “throw ‘errorCode’;” for example:
Set the exits:
exits {
errorWithCsvFile: {
responseType: 'badRequest'
}
}
const csv = await sails.helpers.files.convertToCsv(data)
.intercept(‘somethingWrongCode’, ‘errorWithCsvFile’)
... // Other handles
.intercept(err => new Error(err))
Alternative:
try {
...
const csv = await sails.helpers.files.convertToCsv(data)
.intercept((err) => {
sails.log.error(err)
req.addFlash('error_messages', 'Error parsing data to csv!')
throw 'badRequest';
})
...
} catch (err) {
sails.log.err(err);
return res.redirect(`/`);
}