swift flutterEngine.run(withEntrypoint:“);仍然查找main()而不是标记为@pragma('vm:entry-point')的函数

g6baxovj  于 2023-10-15  发布在  Swift
关注(0)|答案(1)|浏览(158)

我正在尝试将Flutter集成到iOS应用程序中。到目前为止,在文档之后,我可以使用swift的flutterEngine.run();来展示Flutter应用程序。
我现在尝试使用flutterEngine.run(withEntrypoint: "screenAEntrypoint");在lib文件夹中的screen_a.dart中显示ScreenA,但我在Xcode控制台中收到以下消息:

2023-10-03 13:59:59.509135+0200 flutter integration test[78506:757029] [VERBOSE-2:dart_isolate.cc(168)] Could not run the run main Dart entrypoint.
2023-10-03 13:59:59.510175+0200 flutter integration test[78506:757029] [VERBOSE-2:runtime_controller.cc(422)] Could not create root isolate.
2023-10-03 13:59:59.510244+0200 flutter integration test[78506:757029] [VERBOSE-2:shell.cc(614)] Could not launch engine with configuration.

我尝试将文件指定为flutterEngine.run(withEntrypoint: "screenAEntrypoint", libraryURI: "screen_a.dart");,但Xcode找不到它:

2023-10-03 14:17:09.961063+0200 flutter integration test[81661:784695] [VERBOSE-2:shell.cc(91)] Dart Error: Dart_LookupLibrary: library 'screen_a.dart' not found.
2023-10-03 14:17:09.961165+0200 flutter integration test[81661:784695] [VERBOSE-2:dart_isolate.cc(667)] Could not resolve main entrypoint function.
2023-10-03 14:17:09.961228+0200 flutter integration test[81661:784695] [VERBOSE-2:dart_isolate.cc(168)] Could not run the run main Dart entrypoint.
2023-10-03 14:17:09.962495+0200 flutter integration test[81661:784695] [VERBOSE-2:runtime_controller.cc(422)] Could not create root isolate.
2023-10-03 14:17:09.962563+0200 flutter integration test[81661:784695] [VERBOSE-2:shell.cc(614)] Could not launch engine with configuration.

为什么在使用withEntrypoint选项时也要查找main()?它不应该寻找`screenAEntrypoint()吗?
这就是我创建FlutterEngine的方式:

import SwiftUI
import Flutter
import FlutterPluginRegistrant

// Create a Flutter Engine
class FlutterDependencies: ObservableObject {
    let flutterEngine = FlutterEngine(name: "my flutter engine");
  init(){
    // Runs the default Dart entrypoint with a default Flutter route. main.dart
//      flutterEngine.run(); //main.dart entry point

      flutterEngine.run(withEntrypoint: "screenAEntrypoint");
//      flutterEngine.run(withEntrypoint: "screenAEntrypoint", libraryURI: "screen_a.dart");
    // Connects plugins with iOS platform code to this app.
      
      GeneratedPluginRegistrant.register(with: self.flutterEngine);
  }
}

@main
struct flutter_integration_testApp: App {
    // flutterDependencies will be injected using EnvironmentObject.
    @StateObject var flutterDependencies = FlutterDependencies()
      var body: some Scene {
        WindowGroup {
          ContentView().environmentObject(flutterDependencies)
        }
      }

}

这是使用它的视图:

import SwiftUI
import CoreData
import Flutter

struct ContentView: View {
    // Flutter dependencies are passed in an EnvironmentObject.
    @EnvironmentObject var flutterDependencies: FlutterDependencies

    // Button is created to call the showFlutter function when pressed.
    var body: some View {
      Text("iOS View")
        
      Button("Show Flutter!") {
        showFlutter()
      }
    }

  func showFlutter() {
      // Get the RootViewController.
      guard
        let windowScene = UIApplication.shared.connectedScenes
          .first(where: { $0.activationState == .foregroundActive && $0 is UIWindowScene }) as? UIWindowScene,
        let window = windowScene.windows.first(where: \.isKeyWindow),
        let rootViewController = window.rootViewController
      else { return }

      // Create the FlutterViewController.
      let flutterViewController = FlutterViewController(
        engine: flutterDependencies.flutterEngine,
        nibName: nil,
        bundle: nil)
      flutterViewController.modalPresentationStyle = .overCurrentContext
      flutterViewController.isViewOpaque = false

      rootViewController.present(flutterViewController, animated: true)
    }

    
}

这是我想展示的屏幕:

import 'package:flutter/material.dart';

@pragma('vm:entry-point')
void screenAEntrypoint() {
  runApp(const MaterialApp(
    home: ScreenA(),
  ));
}

// @pragma('vm:entry-point')
class ScreenA extends StatelessWidget {
  const ScreenA({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Screen A"),
      ),
      body: const Text("Screen A entry point"),
    );
  }
}
dffbzjpn

dffbzjpn1#

根据这个答案https://stackoverflow.com/a/73412538/9663497如果我使用flutterEngine.run(withEntrypoint: "screenAEntrypoint", libraryURI: "package:my_flutter/screen_a.dart");并将该文件导入main.dart,它确实有效。
因此,https://docs.flutter.dev/add-to-app/ios/add-flutter-screen?tab=no-engine-vc-swiftui-tab的文档似乎已经过时了。

相关问题