英文:
list data API not showing in flutter
问题
以下是您要翻译的代码部分:
class DetailPage extends StatefulWidget {
final Datum posts;
const DetailPage({Key? key, required this.posts}) : super(key: key);
@override
State<DetailPage> createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
Future<List<Mk>> getDetail(String id) async {
String url = Constant.baseURL;
String token = await UtilSharedPreferences.getToken();
final response = await http.get(
Uri.parse('$url/auth/mhs_siakad/kurikulum/detail/$id'),
headers: {
'Authorization': 'Bearer $token',
},
);
print(response.statusCode);
print(response.body);
if (response.statusCode == 201) {
final result = json.decode(response.body) as Map<String, dynamic>;
Data detailKurikulum = Data.fromJson(result['data']);
List<Mk>? l = detailKurikulum.mk;
return l!;
} else {
throw Exception();
}
}
Future<List<Mk>>? futureMk;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const PreferredSize(
preferredSize: Size.fromHeight(kToolbarHeight),
child: CustomAppbar(
title: 'Detail Kurikulum',
),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
headerDetail(),
bodyDetail(),
Text('id: ${widget.posts.name}'),
Text('Title: ${widget.posts.prodi}'),
const SizedBox(
height: 20,
),
const Text('Body: '),
Text(widget.posts.id.toString()),
],
),
),
),
);
}
// 余下的代码未显示,但不影响翻译。
}
代码部分已经翻译完毕。
英文:
I want to display a list based on the ID selected by the user on the detail page but when I try to implement it the data doesn't appear on the detail page and only displays a circularprogressIndicator.
is there something wrong with what i'm doing here?
I'm a bit confused here, and want to display List<Mk> in the widget
Thank You.
class DetailPage extends StatefulWidget {
final Datum posts;
const DetailPage({Key? key, required this.posts}) : super(key: key);
@override
State<DetailPage> createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
Future<List<Mk>> getDetail(String id) async {
String url = Constant.baseURL;
String token = await UtilSharedPreferences.getToken();
final response = await http.get(
Uri.parse(
'$url/auth/mhs_siakad/kurikulum/detail/$id',
),
headers: {
'Authorization': 'Bearer $token',
},
);
print(response.statusCode);
print(response.body);
if (response.statusCode == 201) {
final result = json.decode(response.body) as Map<String, dynamic>;
Data detailKurikulum = Data.fromJson(result['data']);
List<Mk>? l = detailKurikulum.mk;
return l!;
} else {
throw Exception();
}
}
Future<List<Mk>>? futureMk;
// void initState(String id) {
// futureMk = DetailKurikulumProvider.getDetail(id);
// }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const PreferredSize(
preferredSize: Size.fromHeight(kToolbarHeight),
child: CustomAppbar(
title: 'Detail Kurikulum',
),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
headerDetail(),
bodyDetail(),
Text(
'id: ${widget.posts.name}',
),
Text('Title: ${widget.posts.prodi}'),
const SizedBox(
height: 20,
),
const Text('Body: '),
Text(widget.posts.id.toString()),
],
),
),
),
);
}
......
Widget bodyDetail() {
return FutureBuilder<List<Mk>>(
future: futureMk,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Expanded(
child: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 3),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(
1,
2,
),
),
],
),
child: Padding(
padding: const EdgeInsets.all(8),
child: ExpansionTile(
textColor: Colors.black,
iconColor: primaryColor,
collapsedIconColor: primaryColor,
title: Text(snapshot.data![index].matakuliah.toString(),
style: boldTile),
children: <Widget>[
Text(
'Bobot Mata Kuliah (SKS)',
style: bold6,
),
Table(
border: TableBorder.all(color: Colors.grey),
children: [
TableRow(children: [
Column(children: [Text('Wajib', style: bold6)]),
Column(
children: [Text('Pilihan', style: bold6)]),
Column(children: [
Text('Sertifikat', style: bold6)
]),
]),
TableRow(children: [
Column(children: [
Text(
snapshot.data![index].jumlahSksWajib
.toString(),
style: regular6,
textAlign: TextAlign.center)
]),
Column(children: [
Text(
snapshot.data![index].jumlahSksPilihan
.toString(),
style: regular6,
textAlign: TextAlign.center)
]),
Column(children: [
Text(
snapshot.data![index].jumlahSksSertifikat
.toString(),
style: regular6,
textAlign: TextAlign.center)
]),
]),
],
),
],
),
),
),
);
},
),
);
}
return Expanded(
child: Center(
child: CircularProgressIndicator(color: primaryColor),
),
);
},
);
}
}
> i want to show list Mk my model
{
"status": "created",
"code": "201",
"data": {
"kurikulum": [
{
"id": "6c9c9b1b-6188-4080-aa82-a12149388ee4",
"name": "SI20222",
"id_semester": "7f5ad13e-ac75-46fb-8ada-562acec52570",
"smt": "2022-2023 Genap",
"id_prodi": "d4f99a8c-45c3-435b-a3b6-470679bba3bd",
"jenjang": "S1",
"prodi": "Sistem Informasi",
"sks_wajib": 24,
"sks_pilihan": 22,
"total_sks": 46,
"total_sks_wajib": "81",
"total_sks_pilihan": 0,
"total_sks_sertifikat": "4"
}
],
"mk": [
{
"id": "2b2e4042-3ae7-4632-a6e6-ac37e096f7c1",
"id_kurikulum": "6c9c9b1b-6188-4080-aa82-a12149388ee4",
"id_matkul": "d826ab00-fcbf-47c9-95da-9d2a8d628bee",
"kode": "MU0507",
"matakuliah": "Bahasa indonesia",
"sks_tm": "2",
"praktek": 0,
"sks_pl": "0",
"sks_sims": "0",
"semester": 2,
"wajib": "Wajib",
"jumlah_sks_wajib": "2",
"jumlah_sks_pilihan": 0,
"jumlah_sks_sertifikat": 0,
"created_by": "Yunita Sulistiani(2023-01-12 09:12:13)",
"updated_by": "Yunita Sulistiani(2023-01-12 09:12:13)"
},
{
"id": "28495057-40d3-4255-9da1-ecbcbae39a92",
"id_kurikulum": "6c9c9b1b-6188-4080-aa82-a12149388ee4",
"id_matkul": "63acd9d5-9873-4852-bf1a-3b7da133ffc8",
"kode": "DK0511",
"matakuliah": "Sistem operasi",
"sks_tm": "3",
"praktek": 0,
"sks_pl": "0",
"sks_sims": "0",
"semester": 2,
"wajib": "Wajib",
"jumlah_sks_wajib": "3",
"jumlah_sks_pilihan": 0,
"jumlah_sks_sertifikat": 0,
"created_by": "Yunita Sulistiani(2023-01-12 09:12:52)",
"updated_by": "Yunita Sulistiani(2023-01-12 09:12:52)"
},
{
"id": "53dffdf1-793c-41c0-9d6e-6b4e09c6d421",
"id_kurikulum": "6c9c9b1b-6188-4080-aa82-a12149388ee4",
"id_matkul": "bf063878-ab8a-4aad-b686-007a4e21d1b1",
"kode": "MU0501",
"matakuliah": "Kewirausahaan",
"sks_tm": "2",
"praktek": 0,
"sks_pl": "0",
"sks_sims": "0",
"semester": 2,
"wajib": "Wajib",
"jumlah_sks_wajib": "2",
"jumlah_sks_pilihan": 0,
"jumlah_sks_sertifikat": 0,
"created_by": "Yunita Sulistiani(2023-01-12 09:13:17)",
"updated_by": "Yunita Sulistiani(2023-01-12 09:13:17)"
}, ...
答案1
得分: 1
我看到你的未来定义是 null
且从未初始化。
Future<List<Mk>>? futureMk;
而你在 FutureBuilder
中使用了 futureMk
作为一个 future。由于 futureMk
为 null
,snapshot.hasData
永远不会为真。
可能的解决方案:
将 future 更改为定义的那个,或者定义 futureMk
。
FutureBuilder<List<Mk>>(
future: getDetail(<your_id_here>), // 我猜你打算在这里使用这个 future 而不是 futureMk
builder: (context, snapshot) {
if (snapshot.hasData) {
// 这里放置 ListView
...
}
}
英文:
I see your future defination is null
and never initialized.
Future<List<Mk>>? futureMk;
And you are using futureMk
as a future in the FutureBuilder
. As the futureMk
is null
, snapshot.hasData
is never true.
Possible solution:
Change the future to the defined one, or define futureMk
FutureBuilder<List<Mk>>(
future: getDetail(<your_id_here>), //👈 I guess you are intending to use this future here and not futureMk
builder: (context, snapshot) {
if (snapshot.hasData) {
// ListView Here
...
}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论