Dispose() Error
ViewModel 적용 시 여러화면을 빠르게 전환하다 보면 화면을 다 그리지 않은 상태에서 다시 그려야 할 때 아래와 같은 오류가 발생한다.
======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
A ViewModel was used after being disposed.
Once you have called dispose() on a ViewModel, it can no longer be used.
When the exception was thrown, this was the stack:
#0 ChangeNotifier.debugAssertNotDisposed.<anonymous closure> (package:flutter/src/foundation/change_notifier.dart:179:9)
#1 ChangeNotifier.debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:186:6)
#2 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:412:27)
#3 ViewModel.fetchData.<anonymous closure> (package:leute/view/page/refrige_detail_page/refrige_comp_view_model.dart:92:9)
#4 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1325:15)
#5 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1264:9)
#6 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1113:5)
#7 _invoke (dart:ui/hooks.dart:312:13)
#8 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:383:5)
#9 _drawFrame (dart:ui/hooks.dart:283:31)
따라서 dispose() 메서드를 재정의 해줌으로써 다 그렸다는 것을 알려주면 오류를 해결할 수 있다.
class viewModel with ChangeNotifier{
bool _disposed = false;
@override
void dispose() {
_disposed = true;
super.dispose();
}
@override
notifyListeners() {
if (!_disposed) {
super.notifyListeners();
}
}
}