如何解决用sqlite和flutter检索数据
我正在开发Flutter项目并使用Sqflite数据库。我已经设法将数据保存在db中,并且我试图显示保存在sqllite中的数据,我正在尝试使用Listview Builder,但它抛出错误The getter 'length' was called on null.
。但是我正在从数据库中获取数据,这没有问题
错误
The following NoSuchMethodError was thrown building FutureBuilder<List<CartModel>>(dirty,state: _FutureBuilderState<List<CartModel>>#31bfe):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
从数据库中检索数据以进行查看
Widget Coupon_List() {
return FutureBuilder<List<CartModel>>(
future: productFromDatabase(),builder: (context,snapshot) {
if (snapshot.hasData) {
return new ListView.builder(
itemCount: snapshot.data.length,itemBuilder: (context,index) {
return new Column(
crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
new Text(snapshot.data[index].boon_name,style: new TextStyle(
fontWeight: FontWeight.bold,fontSize: 18.0)),new Divider()
]);
});
} else if (snapshot.data.length == 0) {
return new Text("No Data found");
}
return new Container(
alignment: AlignmentDirectional.center,child: new CircularProgressIndicator(),);
},);
}
获取数据库功能以查看
Future<List<CartModel>> productFromDatabase() async {
Future<List<CartModel>> product = dbHandler.getCartList();
return product;
}
用于检索数据的DbHelper类
Future<List<CartModel>> getCartList() async {
List<Map> list = await _db.rawQuery('SELECT * FROM Cart');
List<CartModel> productList = new List();
for (int i = 0; i < list.length; i++) {
var data=list[i];
productList.add(CartModel.fromJson(data));
}
print(productList.length);
return productList;
}
这是我从数据库获取的数据
[{id: 1,boon_id: 3,product_id: 15,boon_name: iPhone 11 Pro Max,product_name: Boon Polo Tshirt,product_price: ₹599.00,quantity: 0,product_size: M,product_color: Blck,product_image: https://www.boonways.com//images/gallery/1593989052.jpg,boon_image: https://www.boonways.com//images/coupon/coupon_15_20200706040643.jpg,percentage: 14.43}]
解决方法
更改此功能
Future<List<CartModel>> productFromDatabase() async {
Future<List<CartModel>> product = dbHandler.getCartList();
return product;
}
到
Future<List<CartModel>> productFromDatabase() async {
List<CartModel> product = await dbHandler.getCartList();
return product;
}
并对此进行更改
if (snapshot.hasData) {
return new ListView.builder(
itemCount: snapshot.data.length,itemBuilder: (context,index) {
return new Column(
crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
new Text(snapshot.data[index].boon_name,style: new TextStyle(
fontWeight: FontWeight.bold,fontSize: 18.0)),new Divider()
]);
});
} else if (snapshot.data.length == 0) {
return new Text("No Data found");
}
进入
if (snapshot.hasData && snapshot.data.length > 0) {
return new ListView.builder(
itemCount: snapshot.data.length,new Divider()
]);
});
} else if (snapshot.hasData && snapshot.data.length == 0) {
return new Text("No Data found");
}
让我知道发生了什么事。
,乍一看,似乎您是在构建器中执行此操作(伪代码):
if there is data then do
return widget
else if the data-length is equal to zero then do
return widget
end if-else
如果没有数据返回并且snapshot.data
为空(else if
部分),这可能会给您带来错误,这可能会解释此消息:
getter'length'被调用为null。
但是我不确定错误发生在哪里。您可能应该更改该条件并进行验证。也许是这样的:
if (snapshot.hasData) {
if(snapshot.data.length == 0) {
return Text('No data');
}
return Text('Data is here');
} else if(snapshot.hasError){
return Text('Something went wrong');
} else {
return CircularProgressIndicator();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。