Manually create and warm up the root Container #211
-
This is a general requirement to warm up a capsule and the capsule should be ready before showing any Flutter-specific widget. For example, a capsule reads a user configuration from shared preferences to create a ThemeData instance. The initial plan would be this. Use a RearchBootstrapper widget, which automatically creates a Container instance, shares it through the widget tree, and disposes the created Container instance. And, use the .toWarmUpWidget extension method to warm the required capsules, and finally construct the MaterialApp. Imagine I have set up a native splash screen. So I do some third-party initialization, e.g. Firebase, Supabase, etc before calling the runApp. So these will keep the native splash screen being shown, as desired. Now it's time to warm up the required capsules, and this is gonna be achieved as part of a custom widget named CapsuleWarmUp (internally utilizes toWarmUpWidget ). So I have to implement a UI for the loading state. I prefer to do the warmup before Flutter draws its first frame, and this means the splash screen will be kept showing. Future<void> main() async {
ensureWidgetsInitialized();
await initializeSupabase();
final container = await setupCapsuleContainer();
restoreSystemUIMode();
runApp(
CapsuleContainerProvider(
container: container,
child: const App(),
),
);
}
Future<CapsuleContainer> setupCapsuleContainer() async {
final container = CapsuleContainer();
await container.warmUp(
[sharedPreferencesReaderWarmUpCapsule],
);
return container;
} This works fine, but the only thing I'm concerned about is the lack of the Container disposal. If I call dispose right after the I would be happy to hear your thoughts. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi 👋 I'll preface this answer with the following (for anyone else stumbling upon this discussion in the future): Note Most of the time you should be using the warm up methodology defined in the docs. That being disclosed, you explicitly said:
Which is a valid use-case that I hadn't thought of before.
Don't worry about it; Flutter doesn't even guarantee that deactivate/unmount will be called on app close (and thus container/other resource disposal that's managed by the widget tree). And since your container will live for the life of the app (since the Your approach looks fine to me, except for: Warning Since you're opting to do this manually, just ensure you also |
Beta Was this translation helpful? Give feedback.
Hi 👋
I'll preface this answer with the following (for anyone else stumbling upon this discussion in the future):
Note
Most of the time you should be using the warm up methodology defined in the docs.
That being disclosed, you explicitly said:
Which is a valid use-case that I hadn't thought of before.
Don't worry about it; Flutter doesn't even guarantee that deactivate/unmount will be called on app close (and thus container/other resource disposal that's managed by the widget tree). And since…