I'm noticing an issue where I can't catch a permission-denied error from cloud firestore with a stream in a flutter app.
I have this repository which is supposed to generate a stream of documents, and I have my security rules set to prevent all reading of documents.
I try catching all Exceptions, and then catching everything, and while breakpoints show an Exception is being thrown for 'permission-denied' from Cloud Firestore, the method never hits either of the catch blocks, and I get a message in VS Code saying I had an unhandled exception. Is there anything I'm missing here?
@override
Stream<List<Habit>> watchMyActiveHabits() async* {
try {
final userDoc = await _firestore.userDocument();
yield* userDoc
.collection('habits')
.withConverter(
fromFirestore: (snapshot, _) => Habit.fromJson(snapshot.data()!),
toFirestore: (habit, _) => habit.toJson(),
)
.where('active', isEqualTo: true)
.snapshots()
.map((snapshot) => snapshot.docs.map((doc) => doc.data()).toList());
} on Exception catch (e) {
_log.e('Exception: $e');
rethrow;
} catch (e) {
_log.e('Unknown error: $e');
rethrow;
}
}
I am using the Flutter bloc pattern and have this bloc that triggers that method and is supposed to return a state if an exception occurs, but it just hangs in the loadInProgress
state:
HabitWatcherBloc(this._habitRepository) : super(const _Initial()) {
on<_Started>((event, emit) {
emit(const HabitWatcherState.loadInProgress());
try {
_habitStreamSubscription =
_habitRepository.watchMyActiveHabits().listen((habits) {
emit(HabitWatcherState.loadSuccess(habits));
});
} on Exception catch (e) {
emit(HabitWatcherState.loadFailure(e));
}
});
}
}