我是 Flutter 的初學者,正在嘗試通過 GET 請求和 FutureBuilder 在小部件中列印醫生串列。所有醫生目前都在本地 MySQL 資料庫中,可以通過Postman訪問,但 GET 請求似乎掛在我的代碼中。
我試過使用Postman提供的代碼片段,似乎也出現了同樣的問題。這是我的代碼:
class _DoctorSelectionScreen extends State<DoctorSelectionScreen> {
DoctorService doctorService = DoctorService();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
drawer: const NavigationDrawerWidget(),
appBar:
AppBar(backgroundColor: Colors.green, title: const Text("Doctors")),
body: FutureBuilder(
future: doctorService.getDoctors(),
builder:
(BuildContext context, AsyncSnapshot<List<Doctor>> snapshot) {
if (snapshot.hasData) {
List<Doctor>? doctors = snapshot.data;
// return Column(children: <Widget>[
// for (var doctor in doctors!) buildDoctor(doctor)
// ]);
return const Center(child: Text("Found doctors"));
} else {
return const Center(child: Text("No doctors found."));
}
}),
),
);
}
class DoctorService {
final String appointmentURL = 'localhost:8082/appointments';
Future<List<Doctor>> getDoctors() async {
print("TEST");
var res = await get(Uri.parse(appointmentURL));
print("TEST2");
if (res.statusCode == 200) {
final obj = jsonDecode(res.body);
List<Doctor> doctors = <Doctor>[];
return doctors;
} else {
throw "Unable to retrieve doctors.";
}
}
}
代碼似乎掛在:
var res = await get(Uri.parse(appointmentURL));
任何指導將不勝感激。
uj5u.com熱心網友回復:
如果你在模擬器上嘗試這個,那么模擬器的本地主機不是127.0.0.0,10.0.2.2所以你需要寫https://10.0.2.2:YOUR_PORT_NO。也不https://127.0.0.0:YOUR_PORT_NO適用于真實設備。
您可以檢查此以獲取更多詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513242.html
標籤:扑弹簧靴镖
上一篇:使用SpringDataMongoRepository進行更新查詢的自定義@Query注釋方法
下一篇:活動與片段
