Singleton
Here’s what happens in your specific case:
Your AppDatabase uses a lazy singleton pattern:
static AppDatabase? _instance;
static AppDatabase get instance => _instance ??= AppDatabase.fromSetup(kFirebaseSetup);
The _instance static field holds the only reference to your singleton.
Yes, Dart has garbage collection that will clean up instances with zero references, including singletons. Here’s what happens:
- Static fields are NOT GC roots by themselves - they don’t automatically keep objects alive
- If no code is actively referencing the singleton, the static field
_instancebecomes the only reference - During garbage collection, if the Dart VM determines that no reachable code can access the singleton, it can be collected
In your Flutter app, the AppDatabase singleton will typically survive because:
- Implicit references: Your app likely has ongoing operations (streams, listeners, cached queries) that hold references to the database
- Framework integration: Firebase/Firestore may maintain internal references
- Active usage: Most database singletons are frequently accessed throughout the app lifecycle
However, if you have periods where:
- All database operations complete
- All streams are closed
- No widgets or services hold references
- No pending futures reference the database
Then the singleton CAN be garbage collected.
If you need to guarantee the singleton persists for the entire app lifecycle, you could:
- Hold a reference in your main app class:
class MyApp extends StatefulWidget {
static late final AppDatabase _db = AppDatabase.instance; // Keeps it alive
// ...
}
- Use a different singleton pattern that creates the instance immediately:
static final AppDatabase _instance = AppDatabase.fromSetup(kFirebaseSetup);
static AppDatabase get instance => _instance;
Your current implementation may be garbage collected if all references are removed, but in practice, this is unlikely in a typical Flutter app due to ongoing database operations and framework integrations. The lazy initialization is generally fine for most use cases.