I have a sqflite database that works, unit tests work. I want to call a sqflite method in onTap method. I understand I cannot execute the sqflite function in the main UI thread (Flutter tells me that at runtime). So I created an Isolate to call the sqflite function, which gives a different error. The Isolate works if I don't call the sqflite function, works if I just return a bool. Here is the code and exception - thanks for any suggestions:
Widget loginButton(BuildContext context) {
return Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 20.0, right: 5.0, top: 20.0, bottom: 0.0),
child: GestureDetector(
onTap: () {
if (emailController.text.length > 1 && passwordController.text.length > 7) {
/// Find user. Then...
doCheckIfFoundUser(emailController.text.trim(), passwordController.text);
} else {
printUserNotFound();
}
},
child: buttonContainer(Colors.indigo, "Login", 20.0),
),
),
);
doCheckIfFoundUser(String email, String password) async {
var result;
List<String> emailPasswordList = new List();
emailPasswordList.add(email);
emailPasswordList.add(password);
var receivePort = new ReceivePort();
Isolate.spawn(callbackFunction, receivePort.sendPort);
SendPort sendPort = await receivePort.first;
var ans = await sendReceive(sendPort, emailPasswordList);
setState(() {
result = ans;
print("The value is $result - please do your thing");
});
}
static void callbackFunction(SendPort callerSendPort) async {
ReceivePort newIsolateReceivePort = ReceivePort();
callerSendPort.send(newIsolateReceivePort.sendPort);
var msg = await newIsolateReceivePort.first;
List<String> emailPasswordList = msg[0];
print("email: ${emailPasswordList[0]}, password: ${emailPasswordList[1]}");
bool foundUser = await searchForUser(emailPasswordList[0], emailPasswordList[1]);
SendPort replyPort = msg[1];
replyPort.send(foundUser);
}
Future sendReceive(SendPort send, message) {
ReceivePort receivePort = ReceivePort();
send.send([message, receivePort.sendPort]);
return receivePort.first;}
I/flutter ( 2073): email: email, password: Passw0rd
E/flutter ( 2073): [ERROR:flutter/runtime/dart_isolate.cc(805)] Unhandled exception:
E/flutter ( 2073): error: native function 'Window_sendPlatformMessage' (4 arguments) cannot be found
E/flutter ( 2073): #0 Window.sendPlatformMessage (dart:ui/window.dart:1089:9)
E/flutter ( 2073): #1 _DefaultBinaryMessenger._sendPlatformMessage (package:flutter/src/services/binary_messenger.dart:85:15)
E/flutter ( 2073): #2 _DefaultBinaryMessenger.send (package:flutter/src/services/binary_messenger.dart:129:12)
E/flutter ( 2073): #3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:309:51)
E/flutter ( 2073):
E/flutter ( 2073): #4 invokeMethod (package:sqflite/src/sqflite_impl.dart:18:34)
E/flutter ( 2073):
E/flutter ( 2073): #5 SqfliteDatabaseFactoryImpl.invokeMethod (package:sqflite/src/factory_impl.dart:33:7)
E/flutter ( 2073): #6 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.safeInvokeMethod. (package:sqflite/src/factory_mixin.dart:22:35)
E/flutter ( 2073): #7 wrapDatabaseException (package:sqflite/src/exception_impl.dart:7:34) E/flutter ( 2073): .
E/flutter ( 2073): #8 SqfliteDatabaseFactoryImpl.wrapDatabaseException (package:sqflite/src/factory_impl.dart:29:7).
E/flutter ( 2073): #9 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.safeInvokeMethod (package:sqflite/src/factory_mixin.dart:22:7). E/flutter ( 2073): #10 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.getDatabasesPath (package:sqflite/src/factory_mixin.dart:136:17). E/flutter ( 2073): E/flutter ( 2073): #11 getDatabasesPath. (package:sqflite/sqflite.dart:166:54) E/flutter ( 2073): #12 UsersSqflite.init (package:himrepo/controller/users_database.dart:20:47). E/flutter ( 2073): . E/flutter ( 2073): #13 _LoginPageState.searchForUser. (package:himrepo/ui/login.dart:268:24). E/flutter ( 2073): . E/flutter ( 2073): #14 _LoginPageState.callbackFunction(package:himrepo/ui/login.dart:166:28). E/flutter ( 2073): . E/flutter ( 2073): #15 _startIsolate.. (dart:isolate-patch/isolate_patch.dart:304:17). E/flutter ( 2073): #16 _RawReceivePortImpl._handleMessage(dart:isolate-patch/isolate_patch.dart:172:12).
E/AndroidRuntime( 6628): FATAL EXCEPTION: Sqflite
E/AndroidRuntime( 6628): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Sqflite
E/AndroidRuntime( 6628): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:794)
E/AndroidRuntime( 6628): at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:727)
E/AndroidRuntime( 6628): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:140)
E/AndroidRuntime( 6628): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:225)
E/AndroidRuntime( 6628): at com.tekartik.sqflite.SqflitePlugin$6.run(SqflitePlugin.java:778)
E/AndroidRuntime( 6628): at android.os.Handler.handleCallback(Handler.java:873)
E/AndroidRuntime( 6628): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 6628): at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime( 6628): at android.os.HandlerThread.run(HandlerThread.java:65)
Thanks to @pskink for helping. The Isolate works, but I was getting exceptions when running sqflite from a Widget onTap event. It turned out that a plugin (json_annotation) I included in my pubspec.yaml was somehow contributing to the exception: "Methods marked with @UiThread must be executed on the main thread. Current thread: Sqflite". After removing that plugin the app works.