我有一个 Dart + Web UI 应用程序,它首先需要从本地 IndexedDB 存储加载数据。IndexedDB API 是异步的,所以我会在加载数据时收到回调。在我的数据库首次打开并准备就绪之前,我不想显示任何 UI 元素。
如何在显示 UI 之前等待数据库初始化?
我有一个 Dart + Web UI 应用程序,它首先需要从本地 IndexedDB 存储加载数据。IndexedDB API 是异步的,所以我会在加载数据时收到回调。在我的数据库首次打开并准备就绪之前,我不想显示任何 UI 元素。
如何在显示 UI 之前等待数据库初始化?
我在我的项目中以 Web UI 方式进行操作:
...
<body>
<template if="showApplication">
<span>The app is ready.</span>
</template>
</body>
...
@observable bool showApplication = false;
main() {
// Initialize...
window.indexedDB.open(...).then((_) {
showApplication = true;
});
}
这还有一个额外的好处:单独的代码/Web组件还可以在依赖数据库连接之前检查应用程序状态等。
隐藏body
标签visibility:hidden
:
<body style="visibility:hidden">
<!-- content -->
</body>
然后在你未来的 then() 回调中显示它
window.indexedDB.open(dbName,
version: version,
onUpgradeNeeded: createObjectStore).then(handleDBOpened);
handleDBOpened(..) {
query('body').style.visibility = "visible"; // <-- show the body tag
}