Flutter 抖动http.get的放置位置
我正在做教室预订系统Flutter 抖动http.get的放置位置,flutter,Flutter,我正在做教室预订系统 class SearchView2 extends StatefulWidget { @override _SearchViewState2 createState() => _SearchViewState2(); } class _SearchViewState2 extends State<SearchView2> { String building = Get.arguments; List data = []; String
class SearchView2 extends StatefulWidget {
@override
_SearchViewState2 createState() => _SearchViewState2();
}
class _SearchViewState2 extends State<SearchView2> {
String building = Get.arguments;
List data = [];
String roomID = "";
int reserved = 0;
int using = 0;
Future<String> getData() async {
http.Response res = await http.get(Uri.parse(
"https://gcse.doky.space/api/schedule/classrooms?bd=$building"));
http.Response res2 = await http.get(Uri.parse(
"https://gcse.doky.space/api/reservation/currtotal?bd=$building&crn=$roomID"));
reserved = jsonDecode(res2.body)["reserved"];
using = jsonDecode(res2.body)["using"];
this.setState(() {
data = jsonDecode(res.body)["result"];
});
return "success";
}
@override
void initState() {
super.initState();
this.getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('강의실 선택')),
body: new ListView.builder(
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
roomID = data[index];
return new Card(
child: ListTile(
onTap: () async {}, title: Text(data[index] + " " + reserved)),
);
},
),
);
}
}
class SearchView2扩展了StatefulWidget{
@凌驾
_SearchViewState2 createState()=>\u SearchViewState2();
}
类_SearchViewState2扩展状态{
字符串building=Get.arguments;
列表数据=[];
字符串roomID=“”;
保留int=0;
int=0;
Future getData()异步{
Response res=wait http.get(Uri.parse(
"https://gcse.doky.space/api/schedule/classrooms?bd=美元);;
http.Response res2=等待http.get(Uri.parse(
"https://gcse.doky.space/api/reservation/currtotal?bd=$building&crn=$roomID”);
reserved=jsonDecode(res2.body)[“reserved”];
using=jsonDecode(res2.body)[“using”];
此.setState(){
data=jsonDecode(res.body)[“结果”];
});
返回“成功”;
}
@凌驾
void initState(){
super.initState();
这是getData();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本('강의실 선택')),
正文:新建ListView.builder(
itemCount:data==null?0:data.length,
itemBuilder:(BuildContext上下文,int索引){
roomID=数据[索引];
归还新卡(
孩子:ListTile(
onTap:()异步{},标题:文本(数据[索引]+“”+保留)),
);
},
),
);
}
}
我想获取“使用”和“保留”数据,并在列表视图中打印它们
但是roomID在Listview中
我希望数据[index]作为roomID,但我的代码roomID将为null,因此它不会打印结果
我应该将http.Response res2移动到哪里?(不是res)
或者在listview中有其他方法获取使用和保留的数据吗?首先,您在该建筑中有一个
建筑和多个房间。因此,将建筑的数据与所有房间的数据一起获取将花费太多时间
相反,你可以把它分成两部分
用于获取建筑数据
Future<List<String>> getData() async {
http.Response res = await http.get(Uri.parse("https://gcse.doky.space/api/schedule/classrooms?bd=$building"));
return (jsonDecode(res.body)["result"] as List)
.map<String>((e) => e.toString())
.toList();
}
现在,您可以使用小部件构建依赖于异步获取数据的东西
您也不需要StatefulWidget
,因为您使用的是FutureBuilder
,并且可以删除您定义的所有不必要的局部变量
这是完整的工作代码
只需将整个SearchView2
代码替换为链接中的代码即可
这是输出
大家好。建筑的价值是什么?
?我用getx来表示建筑价值,这是我以前的观点。所以建筑价值已经初始化,永远不会改变。我明白,我问的是这个价值,这样我就可以复制你的代码,看看有什么数据,这样我就可以为你提供完整的代码来显示ListView
。结果在这里。几分钟后发布答案。`错误:ADB退出,退出代码1执行流式安装ADB:未能安装C:\Users\admin\Desktop\Flatter\frontent\build\app\outputs\Flatter apk\app.apk:cmd:调用服务包失败:管道破裂(32)`错误发生尝试从您的设备卸载应用程序并从代码重新生成。我想将房间和建筑值发送到其他视图。如Get.to(()=>ReservationView(),参数:data[index]);我是否应该列出并放置[building,roomID]是的,应该可以。试试看,如果有什么问题,告诉我。在buildRoomCard
中,您可以访问building
和roomID
。从那里您可以进行操作。
Future<Map<String, dynamic>> getRoomData(String roomID) async {
http.Response res2 = await http.get(Uri.parse("https://gcse.doky.space/api/reservation/currtotal?bd=$building&crn=$roomID"));
return {
'reserved': jsonDecode(res2.body)["success"]["reserved"],
'using': jsonDecode(res2.body)["success"]["using"],
};
}