Advertisement
Nisanth-Reddy

FutureBuilder

May 18th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. class SearchView2 extends StatelessWidget {
  2. String building = "IT대학";
  3.  
  4. Future<List<String>> getData() async {
  5. http.Response res = await http.get(Uri.parse(
  6. "https://gcse.doky.space/api/schedule/classrooms?bd=$building"));
  7. return (jsonDecode(res.body)["result"] as List)
  8. .map<String>((e) => e.toString())
  9. .toList();
  10. }
  11.  
  12. Future<Map<String, dynamic>> getRoomData(String roomID) async {
  13. http.Response res2 = await http.get(Uri.parse(
  14. "https://gcse.doky.space/api/reservation/currtotal?bd=$building&crn=$roomID"));
  15. return {
  16. 'reserved': jsonDecode(res2.body)["success"]["reserved"],
  17. 'using': jsonDecode(res2.body)["success"]["using"],
  18. };
  19. }
  20.  
  21. Widget buildRoomCard(String roomID) {
  22. return FutureBuilder<Map<String, dynamic>>(
  23. future: getRoomData(roomID),
  24. builder: (_, snapshot) {
  25. String using = "loading", reserved = "loading";
  26. if (snapshot.connectionState == ConnectionState.done) {
  27. using = snapshot.data!["using"].toString();
  28. reserved = snapshot.data!["reserved"].toString();
  29. }
  30. return Card(
  31. child: ListTile(
  32. title: Text('Room - $roomID using - $using reserved - $reserved'),
  33. ));
  34. },
  35. );
  36. }
  37.  
  38. @override
  39. Widget build(BuildContext context) {
  40. return Scaffold(
  41. appBar: AppBar(title: Text('강의실 선택')),
  42. body: FutureBuilder<List<String>>(
  43. future: getData(),
  44. builder: (_, snapshot) {
  45. if (snapshot.connectionState != ConnectionState.done ||
  46. snapshot.data == null) return Text('Loading');
  47. return ListView.builder(
  48. itemCount: snapshot.data!.length,
  49. itemBuilder: (BuildContext context, int index) {
  50. return buildRoomCard(snapshot.data![index]);
  51. },
  52. );
  53. }),
  54. );
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement