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

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?

Related

neo4j using transactionListener causes read/write error

I'm trying to use TransactionEventListener in neo4j. There don't seem to be any lifecycle hooks for plugins, so I figure the only way to do it is to have a plugin procedure do it. However, trying to do that gives me this error:
Neo4jError: Writing in read access mode not allowed. Attempted write to internal graph 1 (system)
The plugin uses write mode, even if I'm not actually writing anything to the database; I'm just registering that TransactionEventListener, although that could indeed lead to writes later on. Still, I've got Write mode.
Here's my procedure:
#Procedure(name = "setTransactionListener", mode = Mode.WRITE)
public Stream<BuiltInProcedures.NodeResult> setTaxonomy(
#Name("taxonomy") Map<String, Map<String, Object>[]> taxonomy
) {
var managementService = new DatabaseManagementServiceBuilder(Path.of(".")).build();
var listener = new ValidationTransactionListener(taxonomy);
managementService.registerTransactionEventListener(db.databaseName(), listener);
return null;
}
Best guess is that I'm not supposed to register a transaction listener this way. But if not this way, then how? There don't seem to be any lifecycle hooks that get called when the database starts, so how can I possibly register an transactionEventListener?
Or is there a way I can give myself permission to do this?
What do you actually want to do?
It doesn't work like that, you need to register the listener in the database lifecycle within a KernelExtensionFactory?
See here for an example:
https://github.com/neo4j/apoc/blob/dev/common/src/main/java/apoc/ApocExtensionFactory.java#L53

Add support for returning stream from an isolate

I have started using isolates and returned a value from my isolate as if it was a function.
Then I wanted to change the code to return stream, instead of future.
From this
compute<List<String>, List<String>>(
(params) async { ... return ...;
I wanted to change to this
compute<List<String>, Stream<String>>(
(params) async* { ... yield ...;
But I get the error:
The argument type 'Stream<FutureOr<Stream>>
Function(List)' can't be assigned to the parameter type
'FutureOr<Stream> Function(List)'. (Documentation)
If I understand correctly we can transfer data back "as stream" by opening ports and sending data through it.
Wouldn't it be better if the syntax will stay the same as "Stream function" and the implementation will do that for us?.
When opening ports you need to handle some extra staff like checking if the port is free and changing to a different port number if it doesn't.
Some extra explanation why opening a port is not needed when returning a single value and is needed when returning a stream will be welcome as well.
Should we ask for Dart team to make the change for isolates to support stream as well?.
Related to dart vm send back stream from isolate
I have found isolate_contactor package that can do that.
You can send results from inside the package isolate with
channel.sendResult()
Like a using a yield.

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.

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.

Flutter Firebase Cloud function can not be called

I am getting an error whilst using Firebase Cloud Functions when I try to call a callable function from Flutter.
flutter: caught generic exception
flutter: PlatformException(functionsError, Firebase function failed with exception., {message: NOT FOUND, code: NOT_FOUND})
Here is how I try to call the cloud function with using cloud_functions: ^0.4.2+3
import 'package:cloud_functions/cloud_functions.dart';
_check(String id) async {
HttpsCallable callable = CloudFunctions.instance
.getHttpsCallable(functionName: 'checkUserFavorites');
try {
final HttpsCallableResult result = await callable.call(
<String, dynamic>{
'id': id,
},
);
print(result.data);
} on CloudFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
} catch (e) {
print('caught generic exception');
print(e);
}
}
I have experienced similar issues, and with few days of debugging and experimenting I found the solution only after studying the source code of Cloud Functions Plugin for Flutter.
When you deploy Firebase Cloud function, you can choose any region of preference (closer to your application the better). For example
// using DigitalOcean spaces
exports.generateCloudImageUrl = functions
.region('europe-west3')
.https.onCall((reqData, context) => {
...
}
When you want to call this function from Flutter app, you must specify the region, otherwise all goes to us-central1 which is default. See example code on how to use a function deployed in a specific region
final HttpsCallable generateCloudImageUrl = new CloudFunctions(region: "europe-west3")
.getHttpsCallable(functionName: 'generateCloudImageUrl');
// NB! if you initialize with 'CloudFunctions.instance' then this uses 'us-central1' as default region!
see cloud_function source for init.
Update, as of recent release, you can initialize as below;
FirebaseFunctions.instanceFor(region: "europe-west3").httpsCallable(
"generateCloudImageUrl",
options:
HttpsCallableOptions(timeout: const Duration(seconds: 30)));
Cloud functions are supported in the regions that you are currently running them, according to the Cloud Functions Location Documentation, but not in all regions.
According to what you shared in the comments, I would say that there are 3 cenarios to your issue:
europe-west1: The function is probably out of date, since you are getting an unespected data format error, which suggest that it expects different data/format than your default function.
europe-west2: The function is not deployed in this region, this is hinted in the error message message: NOT FOUND.
Default Function (unknown region): This is the most recent version of the function, on a region different than europe-west1 and europe-west2, and it accepts the call
with the data in the format that you are sending.
NOTE: You can check which regions you currently have your cloud function deployed on the cloud functions dashboard, as you can see on the example image below:
Also, I suspect that the default region you using is us-central1, since according to the documentation:
By default, functions run in the us-central1 region
To fix your issue, I suggest that you redeploy your current version of the function to the europe-west regions that you intend to use.
There are three reasons this error mostly happens:
1. Call the correct function:
Make sure to call the correct function in its full name (visible when you start a local emulator). Espacially if you have additional exports of files in your index.js file make sure to call the export name as well.
Syntax: serverLocation-optionalExportParent-yourFunction
Example: us-central1-post_functions-updateShare
Note that the server location can also be configured in your instance
2. Emulator: Same WIFI
Make sure to be connected to the same wifi, when using the emulator. Otherwise, any call will end in unvailablity resulting in
Unhandled Exception: [firebase_functions/unavailable] UNAVAILABLE
3. Emulator: Correct host configuration
To connect to a physical device the host at all emulators at your firebase.json must be configured: Simply add "host": "0.0.0.0".
Now the host in flutter needs to be your ip-adress of the computer. More on that here
In my case, in addition to the regional issue, what really solved to me was to include the script below in index.html:
<script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-functions.js"></script>