node.js - 从 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
时出现问题。
请帮助我做错了什么。
解决方案
如果@Huthaifa 不是您正在寻找的答案,您是否尝试过更新: 的值localhostUrlLeaveHistoryON
?
如果您正在使用类似的东西:http://localhost
请尝试将其更改为您计算机的 IP 地址。
推荐阅读
- javascript - 返回 if 语句的变量 javascript
- javascript - 如何重命名 PWA 应用程序图标/调试多浏览器的快捷方式
- nginx - Nginx 邮件模块总是 smtp_auth 总是通过 PLAIN 方法
- json - 如何使用 $lte 从 json 中过滤掉字符串值
- javascript - 为什么使用“切片”复制的JS数组不等于其源数组
- python - 当我将 keras 导入 python 时,如何解决这个错误?
- angular - 侧边菜单中的更新指令 - Ionic4/Angular
- pine-script - 显示计算结果而不是输入
- c# - 如何使用 BoardCastReceiver 识别哪张 SIM 卡收到了短信?
- r - 如何在指定日期范围内获取 R 中时间序列的每小时平均值?