I’m building an app in Flutter that uses Firestore as its backend database. I want to implement online syncing only for paid users, while unpaid users can use the local database cache only. I’ve set up a system where I disable network access for unsubscribed users and enable it when they subscribe.
However from my understanding, this is not how it is intended to be used. When an unpaid user subscribes, all their accumulated offline transactions will be synced individually to Firestore, which could result in a large number of transactions.
My Firestore database is structured as User ID > Lists > Items. How can I handle scenarios where only paid users have cloud syncing capabilities? I am sure I could use SQLite for unpaid users and try syncing somehow but I was hoping to just use Firestore to keep it simpler. What is the recommended way to handle a situation like this?
I’ve provided my current implementation below for reference:
ListProvider(this.subscriptionProvider) {
_listsRef = _firestore.collection('lists'); // Initialize here in the constructor body
_initializeDataFlow();
}
Future<void> _initializeDataFlow() async {
subscriptionProvider.addListener(_handleSubscriptionChange);
await _handleSubscriptionChange();
}
Future<void> _handleSubscriptionChange() async {
if (subscriptionProvider.hasActiveSubscription) {
await _firestore.enableNetwork();
print("Network enabled: Syncing with Firestore.");
} else {
await _firestore.disableNetwork();
print("Network disabled: Using local cache only.");
}
_listenToLists();
}
void _listenToLists() {
_isLoading = true;
notifyListeners();
_listsRef.orderBy('orderField').snapshots().listen((snapshot) {
_lists = snapshot.docs.map((doc) => ListModel.fromMap(doc.data() as Map<String, dynamic>)).toList();
_isLoading = false;
notifyListeners();
});
}
@override
void dispose() {
subscriptionProvider.removeListener(_handleSubscriptionChange);
super.dispose();
}