Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在移动设备上使用Floor缓存数据,但也希望为web构建_Flutter_Floor - Fatal编程技术网

Flutter 在移动设备上使用Floor缓存数据,但也希望为web构建

Flutter 在移动设备上使用Floor缓存数据,但也希望为web构建,flutter,floor,Flutter,Floor,我想同时支持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

我想同时支持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);
  }
void loadNews()异步{
发射(加载());
等待newsCacheRepository.init();//由于web上的这一行,数据不会出现
var articles=await newscaherepository.getArticles();
发射(装载(物品));
刷新新闻();
}
Future refreshNews()异步{
var articles=await newsNetworkRepository.getArticles();
发射(装载(物品));
newsCacheRepository.cacheArticles(文章);
}

您也可以使用支持web的Hive。或者,您也可以随时检查平台:

if(!kIsWeb){
 // Your code that doesn't support web
 }
但为了安全起见,您可以将代码包装在try-catch块上


但我更喜欢使用支持web的Hive。

谢谢。成功了。也许将来我会检查蜂巢:)