Supposing that audioplayers|lib/audio_cache.dart
worked only on Android/iOS, I conditionally exclude the following import from a Dart file:
import "package:audioplayers/audio_cache.dart"
in the following way:
import "dart:math" if (dart.library.io) "package:audioplayers/audio_cache.dart";
where "dart:math" can be any fake_stub Dart file. In short this imports a library only for mobile devices in Flutter. Details here (thanks Alois Deniel!).
What would be the best way to hide platform-specific code in Flutter-Web implementation?
import 'dart:io' show Platform;
bool isMobile() => Platform.isAndroid || Platform.isIOS;
class _MyPageState extends State<MyPage> {
dynamic _audioPlayer;
@override
void initState() {
if (isMobile()) {
_audioPlayer = AudioCache(prefix: 'sounds/');
_audioPlayer.load('mysound.mp3');
}
}
}
This naive try fails on AudioCache
reference of course.
Error: Method not found: 'AudioCache'.
_audioPlayer = AudioCache(prefix: 'sounds/');
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…