首页 > 解决方案 > 从 api 获取数据时出现空白屏幕

问题描述

我正在使用 从中获取数据api并将其显示在列表中ListView builder,它在模拟器上正常工作,但是当我在我的 android 手机上运行应用程序时,它显示为空白屏幕,这里看起来。

在此处输入图像描述

在离开历史标签下方,我正在显示来自 api 的数据,但现在看起来像这样。

在模拟器上看起来像这样

在此处输入图像描述

这是代码:

获取 api 数据并将其插入列表

List<leaveHistory> historyList = [];
var loader = 0;
Future<List<leaveHistory>> _getRecord() async {
    Dio dio = new Dio();
    var data = {
      'username': getname,
      'token': getaccesstoken,
    };

    return dio
        .post(localhostUrlLeaveHistoryON, data: json.encode(data))
        .then((onResponse) async {
      Map<String, dynamic> map = onResponse.data;
      List<dynamic> data = map["data"];

      for (var historyData in data) {
        leaveHistory history = leaveHistory(
            historyData["Date"],
            historyData["description"],
            historyData["type"],
            historyData['fromdate'],
            historyData["todate"],
            historyData["noofleavedays"],
            historyData["leave"]);
        historyList.add(history);
        loader = 0;
      }
      if (historyList.length == 0) {
        loader = 1;
      }
      return historyList;
    }).catchError((onerror) {
      loader = 1;
print(onerror.toString());
    });
  }

使用 ListView.builder 显示数据

Widget build(BuildContext context) {
    return Scaffold(
        appBar: new MyAppBar(
            title: Text("Leaves Tracker"),
            onpressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => Profile()));
            }),
        drawer: NavigationDrawerWidget(),
        body: Stack(overflow: Overflow.visible, children: <Widget>[
          Container(
            padding: EdgeInsets.fromLTRB(20, 10, 0, 0),
            child: Text(
              "Leave History",
              style: TextStyle(fontSize: 30, fontFamily: 'RaleWay'),
            ),
          ),
          Container(
            padding: EdgeInsets.fromLTRB(10, 30, 0, 0),
            child: FutureBuilder(
                future: _getRecord(),
                builder: (BuildContext context,
                    AsyncSnapshot<List<leaveHistory>> snapshot) {
                  if (snapshot.hasData) {
                    if (historyList.length != 0) {
                      return Container(
                          padding: EdgeInsets.fromLTRB(0, 30, 0, 0),
                          child: Expanded(
                              child: ListView.builder(
                            itemCount: snapshot.data.length,
                            scrollDirection: Axis.vertical,
                            shrinkWrap: true,
                            itemBuilder: (BuildContext context, int index) {
                              Color c = Colors.red;
                              //changing the avatar color
                              if (i == 0)
                                i++;
                              else if (i == 1) {
                                c = Colors.blue;
                                i++;
                              } else if (i == 2) {
                                c = Color(int.parse(annualboxcolor));
                                i = 0;
                              }

                              Color c1 = Colors.green;
                              if (snapshot.data[index].Status == "Approved") {
                                c1 = Colors.green;
                              } else if (snapshot.data[index].Status ==
                                  "Not Approved") {
                                c1 = Colors.red;
                              } else if (snapshot.data[index].Status ==
                                  "Pending") {
                                c1 = Color(int.parse(pendingrequestcolor));
                              }
                              return SingleChildScrollView(
                                  scrollDirection: Axis.vertical,
                                  child: Column(
                                    children: [
                                      ListTile(
                                          leading: CircleAvatar(
                                              radius: 25,
                                              backgroundColor: c,
                                              child: Container(
                                                padding: EdgeInsets.fromLTRB(
                                                    0, 10, 0, 0),
                                                child:
                                                    Column(children: <Widget>[
                                                  Text(
                                                    snapshot
                                                        .data[index].noofdays
                                                        .toString(),
                                                    style: TextStyle(
                                                        fontSize: 15.0,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        color: Colors.white),
                                                  ),
                                                  Text(
                                                    int.parse(snapshot
                                                                .data[index]
                                                                .noofdays) >
                                                            1
                                                        ? "Days"
                                                        : "Day",
                                                    style: TextStyle(
                                                        fontSize: 10.0,
                                                        color: Colors.white),
                                                  )
                                                ]),
                                              )),
                                          title:
                                              Text(snapshot.data[index].type),
                                          isThreeLine: true,
                                          subtitle: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                snapshot.data[index].fromdate +
                                                    " To " +
                                                    snapshot.data[index].todate,
                                              ),
                                              Text(snapshot.data[index].Status,
                                                  style: TextStyle(
                                                      color: c1,
                                                      fontWeight:
                                                          FontWeight.bold)),
                                              Text(
                                                snapshot
                                                    .data[index].description,
                                              )
                                            ],
                                          )),
                                      Divider(
                                        color: Colors.grey,
                                        height: 10,
                                      ),
                                    ],
                                  ));
                            },
                          )));
                    }
                  }

                  if (loader == 1) {
                    print("run");
                    return Nodatafound();
                  } else {
                    return Center(
                        child: CircularProgressIndicator(
                      color: Colors.blue[500],
                    ));
                  }
                }),
          ),
        ]));
  }
}

历史课

class leaveHistory {
  final String date;
  final String fromdate;
  final String todate;
  final String description;
  final String type;
  final String Status;
  final String noofdays;

  leaveHistory(this.date, this.description, this.type, this.fromdate,
      this.todate, this.noofdays, this.Status);
}

当我从抽屉重定向到此屏幕时,它首先显示此错误

DioError [DioErrorType.response]: Http status error [300]

在这catchError条线上print(onerror.toString());

更新:

当我用数据线将手机与笔记本电脑连接时,它工作正常,但是当我安装apk时出现问题。

请帮助我做错了什么。

标签: node.jsflutterapiandroid-listviewflutter-futurebuilder

解决方案


如果@Huthaifa 不是您正在寻找的答案,您是否尝试过更新: 的值localhostUrlLeaveHistoryON

如果您正在使用类似的东西:http://localhost

请尝试将其更改为您计算机的 IP 地址。


推荐阅读