flutter - Flutter 在移动设备上使用 Floor 缓存数据,但也想为 Web 构建
问题描述
我想同时支持android和web。
但是我使用 Floor 来缓存数据,并且它在 web 上不受支持。所以我只想在 android 上进行数据库调用,我不想在 web 上缓存数据。我想忽略网络上的这些行。
我能以某种方式做到这一点吗?
void loadNews() async {
emit(Loading());
await newsCacheRepository.init(); //because of this line on web the data doesn't appear
var articles = await newsCacheRepository.getArticles();
emit(Loaded(articles));
refreshNews();
}
Future<void> refreshNews() async {
var articles = await newsNetworkRepository.getArticles();
emit(Loaded(articles));
newsCacheRepository.cacheArticles(articles);
}
解决方案
您也可以使用支持 Web 的 Hive。或者您可以随时检查平台:
if(!kIsWeb){
// Your code that doesn't support web
}
但为了安全起见,您可以将代码包装在 try-catch 块中。
但我更喜欢使用支持 web 的 Hive。
推荐阅读
- xamarin - 在运行时更改按钮图标
- python - Django 中的导航链接
- python - 既然 slice 做了浅拷贝,为什么它可以用来更新原始的可变集合?
- qgis - 如何获得经度和纬度以在打印布局中正确显示?
- python - 以编程方式将数据存储在 django 数据库中一次,并在每次服务器启动时排除运行
- python - 如何以可变贴现率贴现现金流并汇总所有现金流
- c# - 如何在 C# 中直接调用方法
- ios - SwiftUI 使用 GeometryReader 时覆盖层的错误垂直对齐
- hl7-fhir - fhir json中的自定义键值对
- flutter - 如何从 Firestore 中检索数据 - Flutter