【发布时间】:2017-12-01 11:39:39
【问题描述】:
我正在尝试在 initState() 期间从 SharedPreferences 获取数据并对其进行测试。 如果我只是在状态中设置私有变量而不调用 setState(),则视图不会更新。
如果我尝试将设置变量包装在 setState() 中,我的测试将中断 在 setState() 方法中使用空指针。
NoSuchMethodError: The method 'markNeedsBuild' was called on null.
Receiver: null
Tried calling: markNeedsBuild()
这是我的 initState()
initState() {
_appRepository.readOnboardingCompleted().then((readValue) {
print("read: $readValue");
setState(()=>_onboardingCompleted = readValue);
});
super.initState();
}
这是测试
test(
'GIVEN app starts WHEN onboarding was completed THEN onboarding is disabled',() {
when(mockAppRepository.readOnboardingCompleted()).thenReturn(mockBoolFuture);
AppState systemUnderTest = new AppState(mockAppRepository);
systemUnderTest.initState();
verify(mockAppRepository.readOnboardingCompleted());
});
我觉得必须有一种更简单的方法来做到这一点。
【问题讨论】:
标签: flutter