Flutter, Bloc如何处理SocketException并查看网速
Flutter, Bloc how to process SocketException and check network speed
在我的应用程序中,我有一些免费数据,用户在第一次打开它时会加载这些数据。我有一个屏幕,我需要从服务器加载一些可以购买的其他数据,如果互联网连接可用,我想处理,如果用户从网络加载数据超过 5 秒,向他们发送下一个状态和显示用户已经加载的数据。
这就是我现在拥有的,如果用户没有互联网连接,CheckListTemplateError 会向他们发送,但我想使用 loadedPaidTemplates
中的数据发送 CheckListTemplateLoaded
在代码注释中我将解释从服务器加载的代码的哪一部分
如果互联网连接可用并且用户从服务器加载数据超过 5 秒,我如何处理,向他们发送本地加载数据的状态?
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());
}
}
我找到了检查互联网连接的代码,但我不确定它是否 100% 正确,所以你应该谨慎使用,但你可以尝试这样的事情:
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());
}
}
在我的应用程序中,我有一些免费数据,用户在第一次打开它时会加载这些数据。我有一个屏幕,我需要从服务器加载一些可以购买的其他数据,如果互联网连接可用,我想处理,如果用户从网络加载数据超过 5 秒,向他们发送下一个状态和显示用户已经加载的数据。
这就是我现在拥有的,如果用户没有互联网连接,CheckListTemplateError 会向他们发送,但我想使用 loadedPaidTemplates
中的数据发送 CheckListTemplateLoaded在代码注释中我将解释从服务器加载的代码的哪一部分
如果互联网连接可用并且用户从服务器加载数据超过 5 秒,我如何处理,向他们发送本地加载数据的状态?
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());
}
}
我找到了检查互联网连接的代码,但我不确定它是否 100% 正确,所以你应该谨慎使用,但你可以尝试这样的事情:
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());
}
}