Men的博客

欢迎光临!

0%

Flitter Riverpod

下面是一个示例,它展示了当异步API准备好时,作用域如何允许您覆盖一个虚拟提供者。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// We don't have an actual instance of SharedPreferences, and we can't get one except asynchronously
final sharedPreferencesProvider =
Provider<SharedPreferences>((ref) => throw UnimplementedError());

Future<void> main() async {
// Show a loading indicator before running the full app (optional)
// The platform's loading screen will be used while awaiting if you omit this.
runApp(const LoadingScreen());

// Get the instance of shared preferences
final prefs = await SharedPreferences.getInstance();
return runApp(
ProviderScope(
overrides: [
// Override the unimplemented provider with the value gotten from the plugin
sharedPreferencesProvider.overrideWithValue(prefs),
],
child: const MyApp(),
),
);
}

Riverpod允许你创建一个ProviderScope,它可以访问父作用域中所有提供者的状态。
return ProviderScope(
parent: ProviderScope.containerOf(context),
child: const AlertDialog(
content: CounterDisplay(),
),
);

https://github.com/SinaSys/flutter_office_furniture_store_app/tree/riverpod

今日工作安排:
把riverpod再深度学习一下,能够熟练运用
构建项目,

今日重点是总结了一些UI布局,希望在后面能有很大的用处。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
when migrating following provider

final currentWebView2Provider = StateProvider<InAppWebViewController?>((ref) => null,);

to a riverpod_generator generated provider that looks like this

@riverpod
class CurrentWebView extends _$CurrentWebView {

@override
InAppWebViewController? build() {
return null;
}
}

The generators enabled autoDispose by default. That's most likely your issue

You can try disabling it with @Riverpod(keepAlive: true)