I am trying to show a list of items in a DropdownField, which requires a non-future list.
So I am trying to turn my future list (which I get from a database call) into a normal list by iterating over it and adding it to a new, normal, list, like so;
When I print the individual elements of the future list, I get results back.
But afterwards, when I print the new list, it returns as a null value.
Does anyone have any idea as to how I can solve this issue ?
PS: I am NOT interested in a FutureBuilder solution. I know that I can show a future list with a futurebuilder, but this will not fit the solution I am hoping to achieve.
Thanks in advance!
That’s because you’re not waiting for the future to complete. When you do print(newList) the future isn’t completed yet.
You will have to need to await the getAnswers data to archieve what you want.
final answers = await getAnswersList();
for (final answer in answers) {
print(answer.toString());
}
The problem is that you are not initializing newList. You are trying to add items to a null list. Change the fourth line of getAll() to the following:
var newList = List<dynamic>();
You are also not awaiting your firebase call. This means that you will print the array BEFORE you finish your firebase call. You need to make this getAll() method async and await, the firebase call so it doesn't print the null version.
Related
I have the following listen function:
FirebaseFirestore.instance.collection('users').doc(widget.userId).snapshots().listen((event)
async{
if(!event.metadata.isFromCache){
String myFirstValue = await event.get('name');
// here i have many local operations that depends on `myFirstValue` Constantly as the
document changes
}
});
In the previous code , after many tests I noticed that I can use await and also I cannot use it like the following:
String myFirstValue = event.get('name');
In all cases I can get correct results.
I am totally confused, should I use await or not ?
What is the better use?
Is there some cases leads me to have errors in my operations that depend on my myFirstValue if I didn't use await?
In other words: does the event that comes from .listen((event) will have all values and there is no need to use await in myFirstValue ?
The event in your code is a DocumentSnapshot. If we check the reference documentation DocumentSnapshot.get does not return a Future, so there's no need to use await when calling it.
That is different for DocumentReference.get(), which does return a Future. Using await here would unwrap the Future and give you the underlying DocumentSnapshot.
I'm still learning about Flutter with firebase.
So, I want to make a to-do-list (TDL) app, that can shows TDL item in the home page.
TDL can have 1 of these 3 status (selesai [finished], belum [not yet], telat [late]),
and i wanted to show the ones which status is belum and telat, or simply the ones which status is not equal to selesai, and has the idProject equal to "0".
Simply i need to show TDL which has idProject = "0" and status!="selesai".
So i wrote this:
static Stream<QuerySnapshot> getDataTDL() {
return CToDoList
.where("status",isNotEqualTo:"selesai")
.where("idProject",isEqualTo: "0")
.snapshots();
}
here's a quick look on my data at firebase.. So, basically it should at least return this one -actually there's few more datas it can return- because it matched my where-clause requirements, but in fact it returns nothing at all.
So what can i do now?
As your query contains both a not equal and an is equal comparison, you will have to create a composite index for the query. If you check the error logs of the flutter process, Firestore should give you a link you could use to create the index. If you can't find the link you would have to manually create it in the Firebase console for the project. More info is here.
I am making a small example with Flutter, the data is saved on Firebase Database Realtime as an array, on the client side I will download the array, display it on a list view then customize it, end the process I want to save directly that array on Firebase. But when I use the following code
myRef.set(myArray);
And error:
Unhandled Exception: Invalid argument: Instance of...
I tried converting my array to json array by the following code
String jsonArray = jsonEncode(myArray);
myRef.set(jsonArray);
then the data was set successfully, but the result was a string that appeared on Firebase, not a list I wanted.
So I can ask questions here, expect a correct answer from everyone, very respectfully
Okay i got an answer
I just create an Map<String,dynamic> to repersent for each item in myArray and put it to List like that
List<Map<String,dynamic>> result = new List<Map<String,dynamic>>();
myArray.forEach((item) {
result.add(item.toJson());
});
And final, i just set 'result' for Firebase
stepRef.set(result);
Thank you for reading :D
I am new to coding and what I am trying to achieve is to set up a method that checks the 'MONDAY' collection inside post and if the data exist? the values of the fields get updated otherwise they get created! I have been stuck for a while now and been redirected to different solutions but nothing has worked for me. I appreciate all help
Future<void> createPostMonday(Post post) async{
await postsRef.document(post.authorId).collection('Monday').setData({
'alOne':post.alOne,
'alTwo':post.alTwo,
'alThree':post.alThree,
'alFour':post.alFour,
'alFive':post.alFive,
'alSix':post.alSix,
'beOne':post.beOne,
'beTwo':post.beTwo,
'beThree':post.beThree,
'beFour':post.beFour,
'beFive':post.beFive,
'beSix':post.beSix,
'likes': post.likes,
'authorId': post.authorId,
'timestamp': post.timestamp,
});
setData() is used to write the contents of a single document whose ID you already know. It requires that you use a DocumentReference object to locate that document.
If you're trying to add a new document to a collection with a unique random ID, you should use add() method of a CollectionReference.
await postsRef.document(post.authorId).collection('Monday').add(...)
I'm trying to fetch an array of strings I have saved on my database but I'm getting the array back with missing values, only the first value is shown inside of it. Here's the structure of my database:
Path: /users/uid/services <-- this is the array
Path: /services/uid <--service document
The code I'm using to retrieve the users is:
_getWorkers() async {
var query = await Firestore.instance.collection('users').where('services', arrayContains: widget.category['uid']).getDocuments();
query.documents.forEach((doc) {
workers.add(doc.data);
List<String> values = List.from(doc.data['services']);
print('services:' + values.toString());
});
var test = await Firestore.instance.collection('users').document('PtBD2EMSTodvlx6WHUx8QOHHLNA2').get();
print('actual services:' + test['services'].toString());
}
Both query and test get data back, but the services array only contains the first value.
Its difficult to answer without actually seeing the entire DB structure. But still on the outset i can see only one error with your code.
When you are trying to execute the where() call, try adding the snapshots to retrieve all relevant data.
Since await is also used, it is much better to call the listen() on it and then read the values to be added to the worker
Try using this below code in place of your first line.
await Firestore.instance.collection('users').where('services', arrayContains: widget.category['uid']).snapshots().listen((query)=>query.documents.forEach((doc)=>print(doc.data['services'])));
For some reason Firebase wasn't returning the updated array. Tested it again today and it worked with the same code. Sorry for bothering, thanks for the help anyways.