_FutureBuilderState <dynamic>在null上调用了getter'length'。接收方:空尝试呼叫:长度

时间:2019-12-07 13:44:55

标签: flutter

我的方法从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);
  }

但是将来的构建器无法显示

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),

//在这里,我也谨防null

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

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

2 个答案:

答案 0 :(得分:0)

您应该在oc describe pods | grep "Name: "中返回jsonData

_getUser()

,然后更改

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在前任为null时执行其后继。

答案 1 :(得分:0)

您的函数不会返回任何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,
                                ),
                              ),
                            );
                          }),
                    ),
                  );
                }
              }),