【问题标题】:Retrieving data with sqlite and flutter使用 sqlite 和颤振检索数据
【发布时间】:2020-08-16 15:21:57
【问题描述】:

我正在开发一个 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;
  }

这是我从 db 获取的数据

[{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}]

【问题讨论】:

    标签: sqlite flutter


    【解决方案1】:

    改变这个功能

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

    让我知道发生了什么。

    【讨论】:

    • 现在错误消失了,但仍然无法正常工作,我在其他部分添加了 circularpageindicator。所以它显示了 circularpageindicator 没有别的。即使数据库中有数据@Tipu
    • 嘿伙计,关于这个的任何想法,还没有想通。 @提普
    • @MorbiusBlack 你打印了你的列表长度吗?
    • [{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}] 这是我运行这个函数时得到的结果getCartList@Tipu
    【解决方案2】:

    乍一看,您似乎是在构建器中执行此操作(伪代码):

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

    【讨论】:

    • 现在错误消失了,但仍然无法正常工作,我在其他部分添加了 circularpageindicator。所以它只显示 circularpageindicator。即使数据库中有数据 @user2352776
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2020-09-10
    • 2021-11-09
    • 2021-02-03
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多