Closed tejainece closed 1 month ago
You can do the trick using conditional imports. This way, the compiler will never import flutter_soloud in your web project. I I think this is a good candidate to include in the official docs.
I got it working like the example below.
Create the following files:
create_audio_service.dart
import 'package:project/core/services/audio/audio_service.dart';
import 'package:project/core/services/audio/create_audio_service_native.dart'
if (dart.library.html) 'package:project/core/services/audio/create_audio_service_web.dart';
AudioService createAudioService() {
return createAudioServiceForPlatform();
}
create_audio_service_web.dart
import 'package:project/core/services/audio/audio_service.dart';
import 'package:project/core/services/audio/no_audio_impl.dart';
AudioService createAudioServiceForPlatform() {
return NoAudioImpl();
}
create_audio_service_native.dart
import 'package:project/core/services/audio/audio_service.dart';
import 'package:project/core/services/audio/soloud_impl.dart';
AudioService createAudioServiceForPlatform() {
return SoLoudImpl();
}
audio_service.dart
abstract class AudioService {
Future<void> init();
void dispose();
void play();
}
Then, I use get_it to inject the proper implemention, importing the create_audio_service
file
locator.registerFactory<AudioService>(() => createAudioService());
I hope it helps.
Hi @daniloapr and @tejainece,
it's not yet on pub.dev, but yesterday I merged in the main
branch the web support.
It would be wonderful if you could try it!
Version 2.1.0 has landed with web support!
I'll close this, thank to you all!
Flutter_Soloud should atleast let the app build for web and be a noop/throw until the Soloud is ported to WASM/Web.