我有一个有效的 sqflite 数据库,单元测试有效。我想在 onTap 方法中调用 sqflite 方法。我知道我无法在主 UI 线程中执行 sqflite 函数(Flutter 在运行时告诉我)。所以我创建了一个 Isolate 来调用 sqflite 函数,它给出了不同的错误。如果我不调用 sqflite 函数,Isolate 就可以工作,如果我只返回一个 bool 就可以工作。这是代码和异常-感谢您的任何建议:
来自 UI 的片段
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),
),
),
);
从 UI 调用的方法
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;}
我/颤振(2073):电子邮件:电子邮件,密码:Passw0rd
E/flutter(2073):[错误:flutter/runtime/dart_isolate.cc(805)]未处理的异常:
E/flutter(2073):错误:找不到本机函数“Window_sendPlatformMessage”(4 个参数)
E/flutter (2073):#0 Window.sendPlatformMessage (dart:ui/window.dart:1089:9)
E/flutter(2073):#1 _DefaultBinaryMessenger._sendPlatformMessage(包:flutter/src/services/binary_messenger.dart:85:15)
E/flutter(2073):#2 _DefaultBinaryMessenger.send(包:flutter/src/services/binary_messenger.dart:129:12)
E/flutter(2073):#3 MethodChannel.invokeMethod(包:flutter/src/services/platform_channel.dart:309:51)
电子/颤振(2073):
E/flutter(2073):#4 调用方法(包:sqflite/src/sqflite_impl.dart:18:34)
电子/颤振(2073):
E/flutter(2073):#5 SqfliteDatabaseFactoryImpl.invokeMethod(包:sqflite/src/factory_impl.dart:33:7)
E/颤振(2073):#6 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.safeInvokeMethod。(包:sqflite/src/factory_mixin.dart:22:35)
E/flutter(2073):#7 wrapDatabaseException(包:sqflite/src/exception_impl.dart:7:34)E/flutter(2073):。
E/flutter(2073):#8 SqfliteDatabaseFactoryImpl.wrapDatabaseException(包:sqflite/src/factory_impl.dart:29:7)。
E/flutter(2073):#9 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.safeInvokeMethod(包:sqflite/src/factory_mixin.dart:22:7)。E/颤振(2073):#10 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.getDatabasesPath(包:sqflite/src/factory_mixin.dart:136:17)。E/flutter(2073):E/flutter(2073):#11 getDatabasesPath。(包:sqflite/sqflite.dart:166:54)E/flutter(2073):#12 UsersSqflite.init(包:himrepo/controller/users_database.dart:20:47)。电子/颤振(2073):。E/颤振(2073):#13 _LoginPageState.searchForUser。(包:himrepo/ui/login.dart:268:24)。电子/颤振(2073):。E/flutter (2073):#14 _LoginPageState.callbackFunction(package:himrepo/ui/login.dart:166:28)。电子/颤振(2073):。电子/颤振(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)。
直接执行 Sqflite 方法给出
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)