-
-
Notifications
You must be signed in to change notification settings - Fork 526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MissingPluginException when using sqflite via flutter_isolate #169
Comments
Thanks for the report. I'm actually working on similar issues so good to have something to test with. I don't know the |
I managed to create a test program that works on Android (could not compile FlutterIsolate on iOS). It requires flutter dev version (isolates in plugin are not supported before I think). I could not manage to find a good way to exchange data using flutter_isolate so basically the only proof is that the can be created from an isolate Test app available here |
@alextekartik you should be able to pass anything to the isolate that you can pass through a SendPort (after fixing the problem @vishna pointed out here) The only real difference between the flutter_isolate and a normal isolate launched in dart is that it gets created from the a platform plugin and has an associated FlutterBackgroundView/FlutterEngine. This is the bit that's needed for method channels to work with plugins. When the flutter_isolate is created it passes a uuid string through the platform plugin that provides the key for a SendPort stored with IsolateNameServer. This is retrieved in the new Isolate and used to pass across the userMessage (which could be another SendPort or anything else you can pass across a Dart SendPort). It should work on iOS in stable. I have just done "flutter channel stable" on OSX and built a test project and it seemed ok. There is some new stuff with regards to background isolates in the dev channel, but I tried to make it work with both. What was the error you got? |
so here's my sample: import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:isolate';
import 'package:flutter_isolate/flutter_isolate.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
void main() {
runApp(MyApp());
_createDBIsolate().then((_) {
print("sending message to db isolate");
_dbSendPort.send("message from main");
});
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Text(""),
);
}
}
SendPort _dbSendPort;
FlutterIsolate _dbIsolate;
Future<void> _createDBIsolate() async {
if (_dbIsolate != null) {
return;
}
ReceivePort receivePort = ReceivePort();
_dbIsolate = await FlutterIsolate.spawn(
_dbMain,
receivePort.sendPort,
);
_dbSendPort = await receivePort.first;
}
void _dbMain(SendPort callerSendPort) {
ReceivePort newIsolateReceivePort = ReceivePort();
callerSendPort.send(newIsolateReceivePort.sendPort);
newIsolateReceivePort.listen((dynamic message) {
print("db isolate got message: $message");
_dbOperation();
});
}
void _dbOperation() async {
print("db in an isolate");
try {
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
// When creating the db, create the table
await db.execute(
'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
// Insert some records in a transaction
await database.transaction((txn) async {
int id1 = await txn.rawInsert(
'INSERT INTO Test(name, value, num) VALUES("some name", 1234, 456.789)');
print('inserted1: $id1');
int id2 = await txn.rawInsert(
'INSERT INTO Test(name, value, num) VALUES(?, ?, ?)',
['another name', 12345678, 3.1416]);
print('inserted2: $id2');
});
} catch (error, stacktrace) {
print(error);
print(stacktrace);
}
} ...and it just works ¯\_(ツ)_/¯ in my sample project. So now I've added bunch more dependencies from my main project and then it breaks with MissingPluginException so seems this is caused by some other plugin - investigating which combination is causing this, will get back at you. |
Anyway, narrowed the issue down to something 🤔 in pinch_zoom_image plugin. Removed it from dependencies and the problem went away. Still new to this flutter thing so unsure what's happening but I guess this issue shouldn't have been opened here. |
This is your problem. It uses activity and that is not available in the isolate. see here, and consider using your own custom registrant which only registers the plugins you need to use in the isolate. ... I'd guess that the list of plugins in Just make a copy of
just bear in mind you will have to manually maintain the list of plugins if you add/remove them as This could easily be fixed without a custom registrant if flutter exposed the list of plugins that |
@rmawatson regarding your questions my issues were:
When trying to build: On iOS I get:
|
yes, this does return an instance of FlutterIsolate, it should probably be specified explictly.
this was an error on my part. I didn't actually try this on the simulator, only on an actual iphone. I will take a look at that! |
Thanks for help. It works perfect now! |
@alextekartik - turns out it was nothing to do with the simulator. I upgraded my flutter install and got the same error as you. Something must have changed in the way the plugins are build, from static to dynamic perhaps. I changed the isolate to find the GeneratedPluginRegistrant dynamically and that seems to have fixed it. Are you able to upgrade your packages in the example and rerun to see if that works? Thanks |
@rmawatson It works! Great! your plugin makes a good solution (I never managed to properly used isolate before with sqflite as I was lost on the registration process) for background data manipulation. Thanks for your update |
@alextekartik Can sqflite be used on Windows? If not, is there any plan to support it? Thank in advance. |
@jason-cao123 Please don't reuse an existing bug on a different issue (you might have got a MissingPluginException but it is not about using isolate). Yes sqflite will be supported on Windows/Linux desktop. Currently the plugin mechanism is not stable and no official plugin supports Windows/Linux neither so I cannot be ahead of what the google team does. As soon as there is a stable API and a way to integrate Windows/Linux into the same package (likely when Flutter Desktop get into alpha quality like MacOS now), I (or someone else) will work on this. |
@alextekartik Thanks for the quick response. Look forward to the new package. |
Unsure if this is expected but the other plugin I use
flutter_secure_storage
works just fine in my flutter isolate whilesqflite
is giving me this error:The text was updated successfully, but these errors were encountered: