I use Flutter, Timer and Firebase/Firestore.batch to do something like this sample code to save data periodically on Mac. https://github.com/tomoyuki28jp/flutterfire_scheduled_batch_write_sample2
When I run my app for few days, I get this error randomly:
flutter: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.
flutter:
#0 MethodChannelWriteBatch.commit
package:cloud_firestore_platform_interface/…/method_channel/method_channel_write_batch.dart:51
<asynchronous suspension>
- After hot reloading, it start working again.
- Background task keep throwing this error until restarting or reloading my app
- While a background task is throwing this error, I can still successfully save firestore data by using my app UI manually.
How can I investigate the cause?
User credentials do expire over time - hot reloading likely re-establishes the Auth session. It is known that Auth needs to be re-established periodically - Firestore is NOT really intended as a permanent connection. Your code needs to be able to respond to changes in Auth state (there is a library function to Listen to Auth events), and respond accordingly - including re-authorizing if that's your intent.