instance property
The singleton instance of SoLoud. Only one SoLoud instance can exist in C++ land, so – for consistency and to avoid confusion – only one instance can exist in Dart land.
Using this static field, you can get a hold of the single instance of this class from anywhere. This ability to access global state from anywhere can lead to hard-to-debug bugs, though, so it is preferable to encapsulate this and provide it through a facade. For example:
final audioController = MyAudioController(SoLoudPlayer.instance);
// Now provide the audio controller to parts of the app that need it.
// No other part of the codebase need import `package:flutter_soloud`.
Alternatively, at least create a field with the single instance of SoLoud, and provide that (without the facade, but also without accessing SoLoud.instance from different places of the app). For example:
class _MyWidgetState extends State<MyWidget> {
SoLoud? _soloud;
void _initializeSound() async {
// The only place in the codebase that accesses SoLoudPlayer.instance
// directly.
final soloud = SoLoudPlayer.instance;
await soloud.initialize();
setState(() {
_soloud = soloud;
});
}
// ...
}
Implementation
static final SoLoud instance = SoLoud._();