我正在努力将Particle Device Setup Library集成到Flutter应用程序中。我已遵循Flutter.io的示例,说明如何集成Platform Channels以将Flutter应用程序与本机代码桥接。 我已经成功打开了与本机Particle SDK的连接,并且可以配置Photon。但是,我不能从这里前进。
我的目的是在从设置阶段成功返回时将用户重定向到新页面。如果设置阶段失败,则用户应该可以重新开始(此功能尚未实现)。
在ParticleSetupState
内的initState()
类中,我叫initiateParticleSetup();
。
我已经初始化了以下变量:
static const platform = const MethodChannel("unique_placeholder/particle-setup");
String _particleSetup = "awaiting";
String particleSetup = "placeholder";
initParticleSetup()
的结构如下:
Future<Null> initiateParticleSetup() async {
String route = "";
Map<String, dynamic> arguments = {};
arguments.putIfAbsent("route", () => route);
print("route: $route");
try {
tempRoute = await platform.invokeMethod("initParticleSetup", arguments);
// Must be a valid route or a parameter passed back from the result of
// the setup phase making the application navigate to the next screen
// upon successful configuration.
particleSetup = route;
} catch (e) {
particleSetup = "Error: $e"; // Should allow the users to configure the setup again.
}
if(!mounted) { return; }
print("After finishing native code: $route"); // This line is causing the the VERBOSE-2 errors messages
}
AppDelegate
包含以下代码:
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController;
let initParticleSetupChannel = FlutterMethodChannel.init(name: "unique_placeholder/particle-setup", binaryMessenger: controller);
initParticleSetupChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: FlutterResult) -> Void in
if ("initParticleSetup" == call.method) {
print("Just entered initParticleSetup")
let argument = call.arguments as? NSDictionary
var route = argument!["route"] as? String
print("initial route: \(route)")
self.initParticleSetup(result: result)
route = "/proceed"
print("route is overwritten: \(route)")
result(route)
print("leaving initParticleSetup")
} else {
result(FlutterMethodNotImplemented);
}
});
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func initParticleSetup(result: FlutterResult) {
if let setupController = ParticleSetupMainController(setupOnly: true) {
print("before self.window? stuff")
self.window?.rootViewController?.present(setupController, animated: true, completion: nil)
print("after self.window? stuff")
}
}
当我完成设置阶段并返回Flutter代码时,控制台中将显示以下错误:
[VERBOSE-2:engine.cc(157)] Could not prepare to run the isolate
[VERBOSE-2:engine.cc(116)] Engine not prepare and launch isolate.
[VERBOSE-2:FlutterViewController.mm(462)] Could not launch engine with configuration.
我怀疑它是由try {} catch {}
方法之后的print()引起的,因为“在完成本机代码之后:$ route”从未打印到控制台。我不知道如何克服这个问题。