【发布时间】:2021-11-07 12:38:30
【问题描述】:
Box not found. Did you forget to call Hive.openBox()? 是调用 Hive.openBox() 的结果。它显示在控制台中。但是,小部件工作正常,并且框的内容正确显示!我的意思是我知道盒子没有打开,这就是我打开它的原因......
错误信息:
======== Exception caught by widgets library =======================================================
The following HiveError was thrown building FutureBuilder<Box<CreditCardOverview>>(dirty, state: _FutureBuilderState<Box<CreditCardOverview>>#d0a4f):
Box not found. Did you forget to call Hive.openBox()?
我的颤振代码:
// ...
Expanded(
child: FutureBuilder(
future: Hive.openBox<CreditCardOverview>('ccOverview'),
builder: (BuildContext context, AsyncSnapshot snapshot) {
// ... builder function checking snapshot etc.
}
// ....
更新 这是状态的完整代码:
class _FinancialsListSmallState extends State<FinancialsListSmall> {
@override
Widget build(BuildContext context) {
final sizeX = MediaQuery.of(context).size.width;
final sizeY = MediaQuery.of(context).size.height - MediaQuery.of(context).viewInsets.bottom;
return SafeArea(
child: Container(
width: sizeX,
height: sizeY,
child: Column(
children: [
PageTitleSmall(titleText: 'My Credit Cards', leadingIcon: Icon(Icons.credit_card)),
Expanded(
child: FutureBuilder(
future: Hive.openBox<CreditCardOverview>('ccOverview'),
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<Widget> children;
if (snapshot.hasData) {
children = <Widget>[
const Icon(
Icons.check_circle_outline,
color: Colors.green,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text('Result: ${snapshot.data}'),
)
];
} else if (snapshot.hasError) {
children = <Widget>[
const Icon(
Icons.error_outline,
color: Colors.red,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text('Error: ${snapshot.error}'),
)
];
} else {
children = const <Widget>[
SizedBox(
child: CircularProgressIndicator(),
width: 60,
height: 60,
),
Padding(
padding: EdgeInsets.only(top: 16),
child: Text('Awaiting result...'),
)
];
}
return ListView(
children: showCreditCardOverview(),
);
},
),
),
],
),
),
);
}
这里发生了什么理想的情况?
【问题讨论】:
-
可能是因为
// ... builder function checking snapshot etc.做错了什么?显示您的完整代码。 -
我更新了消息。对我来说,似乎有些东西出了问题,但我没有看到。 (有点像缺少 await() 左右......)
-
见这里:stackoverflow.com/questions/54594133/… 你应该从你的 FutureBuilder 返回。
-
@Benyamin 谢谢,这个提示帮助了我。我认为问题是多种因素的结合,但我现在解决了。首先,我更改了
FutureBuilder中的第一个if(),并返回了一个创建ListView 的私有方法。其他if statements只是错误处理,显示微调器等。 ListView 的创建也有null safety问题,因为如果数据库为空,构建器可能会返回 null。修复所有这些东西后,它现在可以工作了。
标签: flutter dart flutter-hive