Flutter - Parse Server SDK - IncludeObject not working with liveQuery - flutter

I was trying out includeObject method with my livequery in one of my project. I am using Parse_Server_Sdk for this back4App database. Also, I am using flutter parse_server_sdk latest version 1.0.26.
What i want is i have a list of events. So whenever a new event is added in my database then it should trigger the subscription method. I have used following code and it works as well. But, only thing i have issue with is i don't get the includedobject i have added in QueryBuilder.
QueryBuilder<ParseObject> parseQuery =
QueryBuilder<ParseObject>(ParseObject(tblEvents))
..whereEqualTo(tblEventUserId, currentUser);
parseQuery.includeObject([tblOrganizerId]);
Subscription subscription = await liveQuery.client.subscribe(parseQuery);
subscription.on(LiveQueryEvent.create, (value) {
print("val : ${value}");
print("val : ${value.runtimeType}");
});
Output :
val : {"className":"Group_Events","objectId":"uotQ6BpT4C","createdAt":"2020-08-27T07:18:26.581Z","updatedAt":"2020-08-27T07:18:26.581Z","organizer_id":{"__type":"Pointer","className":"Organizer","objectId":"t9M1oyZHh4"},"user_id":{"__type":"Pointer","className":"_User","objectId":"Hx5xJ5ABxG"},"weight":1}
I/flutter (30335): val : ParseObject
I have also evaluated the expression. But, there is only limited pointer data to "tblOrganizerId". I am not getting whole data of that table.
Can anyone suggest me a workaround or a solution?
Thanks.

I would use the "ParseLiveListWidget". It should be perfect for this scenario.
You can find an example here.
Additional information is in this README.
(Use version 1.0.27 for some important bug fixes.)

Related

Flutter - Riverpod Future Provider: How to keep old data when error occurs

This is my usecase:
data is downloaded
final dataProvider = FutureProvider<MyModel>((ref) async {
return fetchData();
});
and it's used like this within widget's build method:
ref.watch(dataProvider).when(
data: DataWidget(data),
error: ErrorWidget(),
loading: LoadingWidget())
user has option to refresh the data:
ref.refresh(dataProvider.future);
But when there is an error (for example phone is in airplane mode) error is provided so DataWidget is lost and replaced with ErrorWidget... Is there a way using Riverpod to provide/keep existing data instead of error ? I believe it's common scenario, but I didn't find any elegant solution for this problem. I've also read documentation too, but didn't find anything helpful related to this. Am I missing something ? (I'm new to Riverpod) Thank you
Preserving the previous data on refresh is behavior as part of 2.0.0.
Although there were some bugs that you may have encountered. Make sure youre using 2.1.3 or above, which should fix all the issues related to this.
As for using when to show the previous data/error, you can use various flags:
asyncValue.when(
// show previous data/error on loading
skipLoadingOnReload: true,
// show previous data if there's an error
skipError: true,
loading:...,
data:...,
error: ...,
)
In the new version of Riverpod (as of v.2.1.0), you can do this:
asyncValue.when(
skipLoadingOnReload: false,
skipLoadingOnRefresh: true,
skipError: false,
data: DataWidget(data),
error: ErrorWidget(),
loading: LoadingWidget(),
)
You can see more details here.

AWS Amplify How limit the result of observeQuery

I used AWS Amplify datastore and I've make some Graphql requests with the Amplify.DataStore.query() api.
I noticed that, when using the Amplify.DataStore.observeQuery() I couldn't put some limitation on my request and it fetch all the database elements... When using Amplify.DataStore.query() I could.
How can I put a limitation to only watch the update of the last 10 items of a table ?
Here is my code:
Stream<QuerySnapshot<Actuality>> getActualityStream() {
return Amplify.DataStore.observeQuery(
Actuality.classType,
sortBy: [
Actuality.DATE.descending(),
],
);
}
This is actually alot easier than you think. The documentation at https://docs.amplify.aws/lib/datastore/data-access/q/platform/flutter/#predicates clearly shows the predicates you can use.
Example code:
late Stream<QuerySnapshot<Jobs>> streamJobs;
void initState() {
super.initState();
streamJobs =
Amplify.DataStore.observeQuery(Jobs.classType,
where: Jobs.USERID.contains(widget.userId));
}
As for the requirement to only bring back some information. You have two options as far as I can tell. Either generate a field that counds and call a
"where: ElementType.ID.between(1, 5)" dynamically checking the length -10. Else you could try at pagination. Not sure past that. Amplify is rather new.

How to make "compute()" function insert data to sqlite while in isolated process?

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?

Can I use flutter to create an index on firestore?

I'm using the following flutter code to query firestore which orders the data using the field timestamp.
var results = Firestore.instance.collection('post').orderBy('timestamp').getDocuments().then((value) {
var list = value.documents;
return list.map((doc) {
return doc.documentID;
}).toList();
});
When I run this code, it throws the below exception saying an index is required:
W/Firestore(31110): (21.3.0) [Firestore]: Listen for Query(app/jQH7Fp9xCZWYiqZRe7lE/post where readAccess array_contains_any [WzKImODx6WYVqdSW3D9Az3xrUnM2, PUBLIC] order by -timestamp, -name) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/....
The exception even comes with a nice link. When opening that link, a nice UI pops up giving me the ability to create the index, with just a simple click:
Question: simple as the above may seem, I'm not very happy with this. I prefer to be able to create the index from fluttercode. In code I'm looking for something like the below:
Firestore.instance.collection('post').API-TO-CREATE-INDEX('timestamp');
Does it exist? Please advise. Many thanks.
It's not possible to create an index from client apps. You have three main choices:
Clicking the link you already saw.
Using the Firebase CLI to deploy the index from the command line.
Using the gcloud CLI to also deploy from the command line
See also the documentation on managing indexes.

Phonegap and Nova Data Framework -

I am learning PhoneGap for an app project and need to use the database for certain aspects, I am trying out the Nova Data framework,
https://cordova.codeplex.com/wikipage?title=How%20to%20use%20nova.data
I am trying to use my code to put together a test entity, but I am getting a db error telling me there is a missing table. The documentation does not specify that the database should be created beforehand, but I am starting to think that may be the case. Has anyone out there used the Nova framework in a project? I just need a little guidance.
Here is my code I am using to kick off the DB Context:
var DataContext = function () {
nova.data.DbContext.call(this, "HealthDb", "1.0", "Health DB", 1000000);
this.Temperatures = new nova.data.Repository(this, Temperature, "Temperatures");
};
DataContext.prototype = new nova.data.DbContext();
DataContext.constructor = DataContext;
And my entity (Temperature) :
var Temperature = function () {
nova.data.Entity.call(this);
this.Value = 101;
};
Temperature.prototype = new nova.data.Entity();
Temperature.constructor = Temperature;
It is creating an empty database with the proper name, just no tables! I am grateful for any assistance!
Thanks for using our library. I have made the html5 sqlite as a standalone library. Please get it from github.
A live demo link is also available there. And the documentation is more complete. The lib itself has also been updated and a few bugs fixed.
Thanks,
Leo
Turns out I was trying to start up the dbcontext before I defined my entity classes....
Changed the order of my js files and it works.