简体   繁体   中英

Conditional imports and Dart_VLC

I'm working on a Web/Windows/Android app and to play a soundbite I am using just_audio. However, just_audio for windows is deprecated.

To work around this I want to use Dart_VLC for windows only. However, if it is imported at all, the app will crash with the following error (full trace 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. 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. Is there a way to import the windows metronome only on windows (and thus dart_vlc) and generic on all other platforms?

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).

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. As a result it is possible to use conditional imports to direct web towards just_audio; and a runtime Platform.isWindows check to redirect android to JustAudio.

The major downside to this is that the entirety of dart_vlc is compiled into the andoird app as well, I think. Please correct me if I am wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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