【发布时间】:2020-01-16 16:48:25
【问题描述】:
我正在构建一个条形码阅读器,它会在扫描产品的条形码后显示产品的名称。名称和条形码存储在 mongodb 中,我编写了此代码用于连接数据库:
Future<Map> prod() async {
Db db = Db('mongodb://192.168.2.5:27017/database');
await db.open();
DbCollection coll = db.collection('products');
var information = await coll.findOne(where.eq('code', _Barcode));
await db.close();
return information;
}
并且我使用了future builder来显示名称:
FutureBuilder(
future: prod(),
builder: (buildContext, AsyncSnapshot snapshot) {
if ( snapshot.data== null){
return Container(
child: Center(
child: Text("Loading..."),
),
);} else{
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (buildContext, int index) {
return Container(
child:snapshot.data.name,
);
});}
},)
它一直显示加载文本。
【问题讨论】:
-
嗨,Dami,您的意思是
snapshot.data enter code here == null还是格式错误? -
另外,我认为这可能只是您的
prod函数中未捕获的错误。尝试用if (snapshot.hasError) throw snapshot.error; if (!snapshot.hasData) {/* */}替换您的if (snapshot.data == null) /* ... */,看看是否抛出错误
标签: flutter mongo-dart