Resolves #168 Fix error when entity view closed before history loaded

This commit is contained in:
estevez-dev
2019-02-22 15:33:10 +02:00
parent 1a7457abf9
commit 03edaa9ca2

View File

@ -32,6 +32,7 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> {
List _history; List _history;
bool _needToUpdateHistory; bool _needToUpdateHistory;
DateTime _historyLastUpdated; DateTime _historyLastUpdated;
bool _disposed = false;
@override @override
void initState() { void initState() {
@ -47,16 +48,20 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> {
if (_historyLastUpdated == null || now.difference(_historyLastUpdated).inSeconds > 30) { if (_historyLastUpdated == null || now.difference(_historyLastUpdated).inSeconds > 30) {
_historyLastUpdated = now; _historyLastUpdated = now;
ha.getHistory(entityId).then((history){ ha.getHistory(entityId).then((history){
if (!_disposed) {
setState(() { setState(() {
_history = history.isNotEmpty ? history[0] : []; _history = history.isNotEmpty ? history[0] : [];
_needToUpdateHistory = false; _needToUpdateHistory = false;
}); });
}
}).catchError((e) { }).catchError((e) {
Logger.e("Error loading $entityId history: $e"); Logger.e("Error loading $entityId history: $e");
if (!_disposed) {
setState(() { setState(() {
_history = []; _history = [];
_needToUpdateHistory = false; _needToUpdateHistory = false;
}); });
}
}); });
} }
} }
@ -131,4 +136,10 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> {
} }
@override
void dispose() {
_disposed = true;
super.dispose();
}
} }