Dùng try-catch trong hàm async:
try { await api.get(); } catch (e, st) { logger.error(e, st); }- Bọc
Futurechain bằng.catchError():future.catchError((e) => defaultValue). - Với stream, truyền
onErrorvào listen:stream.listen(onData, onError: (e) => handle(e)). - Lan truyền lỗi lên trên; đừng nuốt lỗi âm thầm.
- Trong production, dùng crash reporting (e.g.
- FirebaseCrashlytics) thay vì
print(e)— print chỉ phù hợp khi debug local. - Lỗi không được xử lý sẽ crash app.