【问题标题】:_FutureBuilderState<dynamic>The getter 'length' was called on null. Receiver: null Tried calling: length_FutureBuilderState<dynamic>在 null 上调用了 getter 'length'。接收方:null 尝试调用:长度
【发布时间】:2019-12-07 13:44:55
【问题描述】:

我的方法是从 db 获取数据并显示在控制台上。我也尝试了其他帖子中给出的几个提示,但没有成功。

_getUsers() async {
    print("getting");

    var data = await http.post("http://10.0.2.2/Flutter/getdata.php", body: {
      "date": formattedDate,
    });

    var jsonData = json.decode(data.body);

    print(jsonData);
  }

但是未来的builder无法显示

new FutureBuilder(
              future: _getUsers(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                }
                if (snapshot.hasError) {
                  return Center(
                    child: new Text('Error ${snapshot.error}'),
                  );
                } else {
                  return Center(
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(56.0, 8.0, 56.0, 8.0),

//这里我也防空了

child: ListView.builder(
                          itemCount: snapshot.data.length == null // showing error here
                              ? 0
                              : snapshot.data.length,
                          itemBuilder: (BuildContext context, int index) {
                            return ListTile(
                              leading: new Text(
                                '${snapshot.data[index]["branch"]}',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 25.0,
                                ),
                              ),
                              trailing: new Text(
                                '${snapshot.data[index]["count(`branch`)".toString()]}',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 25.0,
                                ),
                              ),
                            );
                          }),
                    ),
                  );
                }
              }),

无法解决问题。请帮忙..

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您应该在_getUser() 中返回jsonData

    getUsers() async {
        print("getting");
    
        var data = await http.post("http://10.0.2.2/Flutter/getdata.php", body: {
          "date": formattedDate,
        });
    
        var jsonData = json.decode(data.body);
    
        return jsonData;
      }
    

    ,然后改变这个

    itemCount: snapshot.data.length == null // showing error here
                                  ? 0
                                  : snapshot.data.length,
    

    到这里

    itemCount: snapshot.data?.length ?? 0,
    

    snapshot.data? 检查data 是否为空。 ?? 在前任为空时执行其后继。

    【讨论】:

    • 太棒了..非常感谢它解决了我的一天..挣扎了几天..你救了我....
    • @raj 很高兴听到它!
    • 非常感谢@wurikiji,你能详细解释为什么它可以工作(举例),它会有所帮助。否则请指出我的任何文章:)
    【解决方案2】:

    您的函数不返回任何 Future,因此 FutureBuilder 无法让 Future 运行。

    _getUsers() {
        print("getting");
    
        return http.post("http://10.0.2.2/Flutter/getdata.php", body: {
          "date": formattedDate,
        });
    }
    

    它需要返回一个 Future,你不应该使用 await,因为 FutureBuilder 依赖于一个实际的 Future,而不是数据。您在构建器中获取数据,然后对其进行解码。

    new FutureBuilder(
                  future: _getUsers(),
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                    if (snapshot.hasError) {
                      return Center(
                        child: new Text('Error ${snapshot.error}'),
                      );
                    } else if (snapshot.hasData) { // checking for data
                      return Center(
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 56, vertical: 8),
                          child: ListView.builder(
                              itemCount: snapshot.data.length,
                              itemBuilder: (BuildContext context, int index) {
                                return ListTile(
                                  leading: new Text(
                                    '${snapshot.data[index]["branch"]}',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 25.0,
                                    ),
                                  ),
                                  trailing: new Text(
                                    '${snapshot.data[index]["count(`branch`)".toString()]}',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 25.0,
                                    ),
                                  ),
                                );
                              }),
                        ),
                      );
                    }
                  }),
    

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多