In my flutter project i have streams from firebase like this:
final CollectionReference _peopleRef=FirebaseFirestore.instance.collection('people');
List<People> _peopleListFromSnapshot(QuerySnapshot snapshot){
return snapshot.docs.map((doc) {
return People.fromJson(doc);
}).toList();
}
Stream<List<People>> getListAllPeopleStream(){
return _peopleRef.snapshots().map(_peopleListFromSnapshot);
}
Now i would like to receive the data from mongodb and after searching the documentation i guess i have to work with cursors and watch(), but the documentation doesn`t have code examples for flutter/dart.
Also i can`t find a tutorial via google, so i would highly appreciate any kind of help how to convert this stream to an mongodb change stream.
Let me know if you need more information!
Thanks!
Related
I'm working on flutter app that uses php apis for server and sqlite for local data.
The problem is with "compute()".
Here is the explanation :
I have three functions that receives data from api on the server, then add the data to my local database (sqlite) table.
First function to get data from server.
Future<List<Map<String, dynamic>>> getServerData(int vers)async {
//my code
}
Second function to insert data into local database:
Future<int> addNewData(List<Map<String, dynamic>>)async {
//my code
}
Third function to call the first and second function:
Future<bool> checkServerData(int vers)async {
List<Map<String, dynamic>> sdt= await getServerData(vers);
int res=await addNewData(sdt);
if(res>0) return true;
else return false;
}
I want to call the third function in a compute function:
compute(checkServerData, 2);
When did that I found this error:
null check operator used on null value.
Note*:
If I used it without calling local database it works good.
The error appears if I called the database to insert data into.
When I searched about this issue I found that it's not allowed to access any resources which generated in one thread from another thread. But I didn't understand exactly how to resolve it or how to use another way that do the same idea.
After searching about the issue specified, I found those workaround solutions:
1: if the process is very important to work in background, you can use the Isolate package classes and functions which allow all isolated processes or contexts to share data between them as messages sending and receiving. But it's something complex for beginners in flutter and dart to understand these things, except those who know about threading in another environments.
To now more about that I will list here some links:
Those for flutter and pub documentation:
https://api.flutter.dev/flutter/dart-isolate/dart-isolate-library.html
https://api.flutter.dev/flutter/dart-isolate/Isolate-class.html
https://pub.dev/packages/flutter_isolate
This is an example in medium.com website:
https://medium.com/flutter-community/thread-and-isolate-with-flutter-30b9631137f3
2: the second solution if the process isn't important to work on background:
using the traditional approaches such as Future.builder or async/await.
You can know more about them here:
https://www.woolha.com/tutorials/flutter-using-futurebuilder-widget-examples
https://dart.dev/codelabs/async-await
and you can review this question and answers in When should I use a FutureBuilder?
I want to get data from firebase cloud
Data means my chatApp's messages.
but in my code i got error
android studio suggest me to add nullcheck(!) to
final messages = snapshot.data!.documents;
but it not working
Try to add the null-check and fix your typo: messgeText => messageText.
You r using Flutter 2.5.0 and now for the latest flutter version the syntax are changed for Firebase . Try this way:
final message = snapshot.data!.data()
I have a class for Player that has nextDate, loginDate and saveDate.
Below code works fine and able to retrieve the fields.
DocumentSnapshot documentSnapshot =
await db.collection(Paths.playersPath).doc(uid).get();
Player player = Player.fromFirestore(documentSnapshot);
print('print ${player.nextDate}');
print('print ${player.loginDate}');
print('print ${player.saveDate}');
My question now is if it is possible to use a variable for the nextDate, loginDate and saveDate so that I can just pass which date should I retrieve.
I tried the following but it is not working.
String dateName = 'nextDate';
DocumentSnapshot documentSnapshot =
await db.collection(Paths.playersPath).doc(uid).get();
Player player = Player.fromFirestore(documentSnapshot);
print('print ${player.$dateName}');
Please help. Thanks!
As far as the code snippet is concerned, I can see you have already defined a method fromFirebase to serialize it, in that case you must extract the data payload from the DocumentSnapshot provided by the Firebase DB by using the data method provided by the DocumentSnapshot class. The problem in your case is you're trying to serialize the DocumentSnapshot itself which is not a class of type Map<String, dynamic> which is need for json serialization. If you take a look at the docs, the data method provides a Map<String, dynamic>
Another alternative could be
If you are using FlutterFire plugins that are officially developed by the Firebase and Flutter teams, you can use the withConverter method which can serialize the response for you to the Player class in your case. Here you can find more info about the same.
unluckily, in Flutter mirrors are not available.
You can look it up in the official documentation about this matter.
Nonetheless, the most straightforward workaround is using a Map, so that the keys in that map are your options, and the values the actual property you want to use.
There's a good example in this previous answer.
In my code,
Stream<List<Reminder>> getReminders() {
return _db.collection('reminders').where('subject',isEqualTo: "SCM").orderBy('due',descending: true).snapshots().map((snapshot) => snapshot
.docs
.map((document) => Reminder.fromFirestore(document.data()))
.toList());
}
When I use where or orderBy separately, I can see the list is reactive, whenever I change something in Firestore, reflects immediately in the App in realtime. If I combine them like above, the app doesn't react to the change in realtime. What am I doing wrong?
Thanks.
I want to implement a search user engine on my app. It is writen with swift and has a Firebase backend. The problem is that I don't find any help on the web.
My data is structured like this:
https://i.stack.imgur.com/N7qbT.png
I tried with this code and others, but still don't working:
Database.database().reference().queryOrdered(byChild: "fullname").queryEqual(toValue: "michel dupond").observe(.childAdded, with: { (snapshot) in
print("Snapshot:", snapshot as? [String: String])
}
Will your search always contain a full name? If you want to implement something like contains(value: String) I'd recommend using Algolia to add string searches.