1

So I am using the Flutter game template, which I found this:

 ProxyProvider2<SettingsController, ValueNotifier<AppLifecycleState>,
              AudioController>(
            // Ensures that the AudioController is created on startup,
            // and not "only when it's needed", as is default behavior.
            // This way, music starts immediately.
            lazy: false,
            create: (context) => AudioController()..initialize(),
            update: (context, settings, lifecycleNotifier, audio) {
              if (audio == null) throw ArgumentError.notNull();
              audio.attachSettings(settings);
              audio.attachLifecycleNotifier(lifecycleNotifier);
              return audio;
            },
            dispose: (context, audio) => audio.dispose(),
          ),

But unfortunately, I don't like provider. How to convert this provider to riverpod?

My Car
  • 4,198
  • 5
  • 17
  • 50
  • Hello Mosh. Did you get this working eventually? I'm folloing the same architecture and I have hit a snag – DARTender Feb 13 '23 at 22:06

2 Answers2

2

An equivalent in Riverpod would be:

final settingsProvider = Provider<SettingsController>(...);
final appLifecycleStateProvider = Provider<ValueNotifier<AppLifecycleState>>(...);


final audioControllerProivder = Provider<AudioController>((ref) {
  final audio = AudioController()..initialize();
  ref.onDispose(audio.dispose);

  ref.listen<SettingsController>(settingsProvider, (prev, next) {
    audio.attachSettings(next);
  });
  ref.listen<ValueNotifier<AppLifecycleState>>(appLifecycleStateProvider, (prev, next) {
    audio.attachLifecycleNotifier(next);
  });

  return audio;
});

Although it's possible that there are ways to simplify this further by using ref.watch. But the snippet above should be a good start

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
0

Remi Rousselet

Using the ref.listen is not working, What I did was change it into ref.watch

  • That's not the same thing as what you described in your question. Using `ref.watch`, your state will reset when the dependency changes – Rémi Rousselet Oct 26 '22 at 12:48