简体   繁体   English

条件导入和 Dart_VLC

[英]Conditional imports and Dart_VLC

I'm working on a Web/Windows/Android app and to play a soundbite I am using just_audio.我正在开发 Web/Windows/Android 应用程序并播放我正在使用的 just_audio 的声音片段。 However, just_audio for windows is deprecated.但是,windows 的 just_audio 已弃用。

To work around this I want to use Dart_VLC for windows only.要解决此问题,我只想将 Dart_VLC 用于 windows。 However, if it is imported at all, the app will crash with the following error (full trace https://pastebin.com/qNrW3ghK ):但是,如果完全导入,应用程序将崩溃并出现以下错误(完整跟踪https://pastebin.com/qNrW3ghK ):

LateInitializationError: Field 'dynamicLibrary' has not been initialized.

I've found that conditional imports resolve this to an extent.我发现有条件的导入在一定程度上解决了这个问题。

import 'metronome_finder.dart'
    if (dart.library.io) 'windows_metronome.dart'
    if (dart.library.js) 'generic_metronome.dart';

This (and an abstract, two classes, and a stub) works for Web/Windows.这(以及一个抽象、两个类和一个存根)适用于 Web/Windows。 However, on android, it appears dart.library.io is loaded and dart_vlc will be imported as well, crashing the app with the above error.但是,在 android 上,似乎加载了 dart.library.io 并且还将导入 dart_vlc,导致应用程序崩溃并出现上述错误。 Is there a way to import the windows metronome only on windows (and thus dart_vlc) and generic on all other platforms?有没有办法只在 windows(以及 dart_vlc)上导入 windows 节拍器并在所有其他平台上通用?

Alternatively is there another cleaner way to resolve this issue?或者还有另一种更清洁的方法来解决这个问题吗?

I've also asked this question on reddit and thanks in part to /u/bsutto there is now a solution (or a workaround).我也在reddit上问过这个问题,部分感谢 /u/bsutto 现在有一个解决方案(或解决方法)。

I've written an example based on my own project.我已经根据自己的项目编写了一个示例 The core revelation here is that the import of dart_vlc breaks compilation on web, but the initialization of dart_vlc crashes during runtime.这里的核心启示是 dart_vlc 的导入破坏了 web 上的编译,但是 dart_vlc 的初始化在运行时崩溃了。 As a result it is possible to use conditional imports to direct web towards just_audio;因此,可以使用条件导入将 web 指向 just_audio; and a runtime Platform.isWindows check to redirect android to JustAudio.和运行时 Platform.isWindows 检查以将 android 重定向到 JustAudio。

The major downside to this is that the entirety of dart_vlc is compiled into the andoird app as well, I think.我认为,这样做的主要缺点是整个 dart_vlc 也被编译到 andoird 应用程序中。 Please correct me if I am wrong.如果我错了,请纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM