我已经使用缓存的颤振引擎进入了现有的本机应用程序(添加到应用程序)。
override fun onCreate(savedInstanceState: Bundle?) {
Log.d(TAG, "onCreate")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
context = this.application.applicationContext
if (!FlutterEngineCache.getInstance().contains(
FLUTTER_ENGINE
)
) {
mFlutterEngine = FlutterEngine(context)
mFlutterEngine.dartExecutor
.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
FlutterEngineCache.getInstance()
.put(FLUTTER_ENGINE, mFlutterEngine)
}
//Create Flutter Fragment
mFragmentManager = supportFragmentManager
mFlutterFragment = mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT) as FlutterFragment?
if (mFlutterFragment == null) {
mFlutterFragment =
FlutterFragment.withCachedEngine(FLUTTER_ENGINE).transparencyMode(FlutterView.TransparencyMode.opaque).build()
mFragmentManager
.beginTransaction()
.add(R.id.fragment_container, mFlutterFragment as Fragment, FLUTTER_FRAGMENT)
.commit()
} else {
mFragmentManager
.beginTransaction()
.show(mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT)!!)
.commit()
}
}
Run Code Online (Sandbox Code Playgroud)
我在生产应用程序中遇到以下崩溃
Caused by: java.lang.IllegalStateException: The requested cached FlutterEngine did not exist in the FlutterEngineCache: 'FLUTTER_ENGINE'
at …Run Code Online (Sandbox Code Playgroud) 我已将颤振添加到现有的 Android 应用程序中。使用预热颤振引擎。
var flutterEngine = FlutterEngineCache.getInstance().get(Constants.FLUTTER_ENGINE)
if (flutterEngine == null) {
flutterEngine = FlutterEngine(this)
FlutterEngineCache.getInstance().put(Constants.FLUTTER_ENGINE, flutterEngine)
flutterEngine.navigationChannel.setInitialRoute(
"route1"
)
flutterEngine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
}
Run Code Online (Sandbox Code Playgroud)
当我销毁颤振引擎时, FlutterEngineCache.getInstance().get(Constants.FLUTTER_ENGINE)?.destroy()
出现以下错误。
java.lang.RuntimeException: Unable to destroy activity: java.lang.RuntimeException: Cannot execute operation because FlutterJNI is not attached to native.
Caused by: java.lang.RuntimeException: Cannot execute operation because FlutterJNI is not attached to native.
at io.flutter.embedding.engine.FlutterJNI.ensureAttachedToNative(FlutterJNI.java:222)
at io.flutter.embedding.engine.FlutterJNI.onSurfaceDestroyed(FlutterJNI.java:315)
at io.flutter.embedding.engine.renderer.FlutterRenderer.stopRenderingToSurface(FlutterRenderer.java:207)
at io.flutter.embedding.android.FlutterView.detachFromFlutterEngine(FlutterView.java:795)
at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDestroyView(FlutterActivityAndFragmentDelegate.java:442)
at
Run Code Online (Sandbox Code Playgroud) 所以我试图在单击按钮时打开一个简单的网络视图。
这是我的 Scaffold 小部件的主体:
body:
WebView(
initialUrl: "https://www.google.com/",
javascriptMode: JavascriptMode.unrestricted,
)
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我独立运行这个 flutter 项目时,它成功地在 iOS 模拟器中打开了 Webview。但是当我将此 flutter 模块集成到现有的 iOS 应用程序中时,它不起作用(显示空白屏幕)。为了添加颤振模块,我遵循了此链接,模块的其他部分工作正常。
我已经在 flutter 模块的 info.plist 中设置了这个:
<key>io.flutter.embedded_views_preview</key>
<true/>
Run Code Online (Sandbox Code Playgroud)
我在文件中添加了以下版本pubspec.yaml:
webview_flutter: ^0.3.22+1
Run Code Online (Sandbox Code Playgroud) 我能找到的 Flutter 的 Add-to-App 的所有文档都演示了如何推送/继续/呈现新创建的 FlutterViewController。我需要将颤动视图添加到选项卡栏中的选项卡。我尝试将班级的超类更改为UIViewController有效FlutterViewController,但没有使用应用程序委托中创建的颤动引擎。此解决方案导致在启动屏幕中显示颤动视图之前,这是不希望的。
如何向 中添加颤动视图UIViewController?或者如何将引擎分配给FlutterViewController类内的引擎?