将 flutter adMob 功能从提供程序转换为 riverpods

Converting flutter adMob functionality from provider to riverpods

我正在将我的整个应用程序迁移到 riverpods 并遇到一个持久错误。本质上,在我的 main.dart 中,我曾经 Provider.value 这样:

final adState = AdState(initialization: adsInitialization);

runApp(
  Provider.value(
    value: adState,
    child: MyApp(email, password, language),
  ),
);

'''

现在我有

 runApp(ProviderScope(
  child: MyApp(email, password, language),
));

如 riverpods 文档中所述。我想知道需要修改什么才能像以前一样通过 adstate 'value'?我对 Provider.value 一开始所做的事情感到有点困惑...

这是我得到的错误

flutter: Error: Could not find the correct Provider<UserSettings> above this HomePage Widget

This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:

- You added a new provider in your `main.dart` and performed a hot-reload.
  To fix, perform a hot-restart.

- The provider you are trying to read is in a different route.

  Providers are "scoped". So if you insert of provider inside a route, then
  other routes will not be able to access that provider.

- You used a `BuildContext` that is an ancestor of the provider you are trying to read.

  Make sure that HomePage is under your MultiProvider/Provider<UserSettings>.
  This usually happens when you are creating a provider and trying to read it immediately.

感谢任何帮助,谢谢!

Provider<AdState>.value(value: adState) 的 Riverpod 等价物是 Provider<AdState>((ref) => adState)。但是,我会在提供程序内部初始化实例。

final adState = Provider<AdState>((ref) {
  return AdState(initialization: adsInitialization);
});