从本地打开两个flutter模块

问题描述 投票:0回答:1

我正在尝试从我们的本机iOS应用程序中打开两个不同的flutter模块,该模块具有许多功能,可快速且有效地编写。现在我们要在Flutter中建立两个新功能。为此,我们需要从本地iOS代码中打开两个不同的模块或抖动方法。

我尝试过许多解决方案,但没有运气。这是我创建的两个示例颤振方法,将来会触发两个不同功能的路由:

//First Method
class MyFirstApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to My First App'),
        ),
        body: Center(
          child: Text('Hello World 1'),
        ),
      ),
    );
  }
}

//Second Method
class MySecondApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to My Second App'),
        ),
        body: Center(
          child: Text('Hello World 2'),
        ),
      ),
    );
  }
}

这里是我尝试用颤动写的一个route

class RouteState extends StatefulWidget {
  RouteState({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _RouteState createState() => _RouteState();
}

class _RouteState extends State<RouteState> {
  static const apiChannel = const MethodChannel('my_channel');

  Future<dynamic> handleMethod(MethodCall call) async {
    switch(call.method) {
      case 'first':
        return MyFirstApp();
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text('Hello World 0'),
      ),
    );
  }
}

现在来自iOS,我正在尝试在本机代码库的viewcontroller中打开上述任何一种方法。

let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
let channel = FlutterMethodChannel(name: "my_channel", binaryMessenger: flutterViewController.binaryMessenger)
channel.invokeMethod("first", arguments: "")
//viewController.show(flutterViewController, sender: viewController)

但是没有任何反应,没有从这里打开任何东西。也不会打印任何错误的日志。我已经在两侧进行调试,断点可以到达iOS端的所有代码,而在颤动端则一切正常。当我仅使用main()运行Flutter模块时,它可以工作。但是不能从本地打开。

ios flutter module communication
1个回答
0
投票

您未在代码中调用present。如前所述here

尝试一下:

let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
    let flutterViewController =
        FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
    present(flutterViewController, animated: true, completion: nil)
© www.soinside.com 2019 - 2024. All rights reserved.