使用sqlite和flutter检索数据



我正在处理一个Flutter项目,并使用Sqflite数据库。我已经设法将数据保存在数据库中,并试图显示保存在sqllite中的数据,我正在尝试使用listview生成器,但它抛出了错误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,
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.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部分(,这可能会给您带来错误,这可以解释以下消息:

在null上调用了getter"length"。

但我不确定错误发生在哪里。您可能应该更改该条件并进行验证。也许是这样的:

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();
}

最新更新