In my application I have some free data that the user loads when they open it for the first time. I have screen where I need to load some other data from the server, that can be purchased, and I want to process if internet connection is available and also if user loading data from network for over 5 seconds, emit to them the next state and display data that user already loaded.
This is what I have right now, if user doesn't have internet connection, CheckListTemplateError emits to them, but I want to emit CheckListTemplateLoaded with data from loadedPaidTemplates
in code comment I will explain which part of code loading from server
How I can process if internet connection is available and if user loading data from server for over 5 seconds, emit to them the state with local loaded data?
Future<void> _loadCheckListTemplates(OnCheckListTemplateLoad event, Emitter emit) async {
emit(CheckListTemplateLoading());
try {
// this is what I load from local data
final List<CheckListTemplate> loadedTemplates = await _checkListRepository.getLocalCheckListTempaltes();
// this is loading from server
final List<CheckListTemplate> loadedPaidTemplates = await _checkListRepository.getPaidCheckListTempaltes();
for (int i = 0; i < loadedPaidTemplates.length; i ) {
loadedTemplates.add(loadedPaidTemplates[i]);
}
final List<String> loadedPaidCheckListIds = await _checkListRepository.getPaidCheckListTemplateData();
emit(CheckListTemplateLoaded(loadedTemplates: loadedTemplates, paidCheckListsIds: loadedPaidCheckListIds));
} catch (e) {
emit(CheckListTemplateError());
}
}
CodePudding user response:
I found code for checking internet connection, but I'm not sure if it's 100% correct, so you should take it with caution, but you can try something like this:
Future<void> _loadCheckListTemplates( OnCheckListTemplateLoad event, Emitter emit) async {
emit(CheckListTemplateLoading());
try {
// this is what i load from local data
final List<CheckListTemplate> loadedTemplates =
await _checkListRepository.getLocalCheckListTempaltes();
bool _isConnectionSuccessful = false;
// check for internet connection
final response = await InternetAddress.lookup('www.google.com');
_isConnectionSuccessful = response.isNotEmpty;
if (!_isConnectionSuccessful) {
// do something if there is no internet connection or in your catch block
}
// initializing your 5 second timer before your next request
final timer = Timer(Duration(seconds: 5), () {
// do something if your request takes more then 5 seconds
});
// this is loading from server
final List<CheckListTemplate> loadedPaidTemplates =
await _checkListRepository.getPaidCheckListTempaltes();
//stop the timer if previous request finished in less then 5 seconds
if (timer.isActive) timer.cancel();
for (int i = 0; i < loadedPaidTemplates.length; i ) {
loadedTemplates.add(loadedPaidTemplates[i]);
}
final List<String> loadedPaidCheckListIds =
await _checkListRepository.getPaidCheckListTemplateData();
emit(CheckListTemplateLoaded(
loadedTemplates: loadedTemplates,
paidCheckListsIds: loadedPaidCheckListIds));
} catch (e) {
emit(CheckListTemplateError());
}
}