列表数据API在Flutter中未显示

huangapple go评论67阅读模式
英文:

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&lt;DetailPage&gt; createState() =&gt; _DetailPageState();
}

class _DetailPageState extends State&lt;DetailPage&gt; {
  Future&lt;List&lt;Mk&gt;&gt; getDetail(String id) async {
    String url = Constant.baseURL;
    String token = await UtilSharedPreferences.getToken();
    final response = await http.get(
      Uri.parse(
        &#39;$url/auth/mhs_siakad/kurikulum/detail/$id&#39;,
      ),
      headers: {
        &#39;Authorization&#39;: &#39;Bearer $token&#39;,
      },
    );
    print(response.statusCode);
    print(response.body);
    if (response.statusCode == 201) {
      final result = json.decode(response.body) as Map&lt;String, dynamic&gt;;
      Data detailKurikulum = Data.fromJson(result[&#39;data&#39;]);
      List&lt;Mk&gt;? l = detailKurikulum.mk;
      return l!;
    } else {
      throw Exception();
    }
  }

  Future&lt;List&lt;Mk&gt;&gt;? 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: &#39;Detail Kurikulum&#39;,
        ),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              headerDetail(),
              bodyDetail(),
              Text(
                &#39;id: ${widget.posts.name}&#39;,
              ),
              Text(&#39;Title: ${widget.posts.prodi}&#39;),
              const SizedBox(
                height: 20,
              ),
              const Text(&#39;Body: &#39;),
              Text(widget.posts.id.toString()),
            ],
          ),
        ),
      ),
    );
  }

......

  Widget bodyDetail() {
    return FutureBuilder&lt;List&lt;Mk&gt;&gt;(
      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: &lt;Widget&gt;[
                          Text(
                            &#39;Bobot Mata Kuliah (SKS)&#39;,
                            style: bold6,
                          ),
                          Table(
                            border: TableBorder.all(color: Colors.grey),
                            children: [
                              TableRow(children: [
                                Column(children: [Text(&#39;Wajib&#39;, style: bold6)]),
                                Column(
                                    children: [Text(&#39;Pilihan&#39;, style: bold6)]),
                                Column(children: [
                                  Text(&#39;Sertifikat&#39;, 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

{
    &quot;status&quot;: &quot;created&quot;,
    &quot;code&quot;: &quot;201&quot;,
    &quot;data&quot;: {
        &quot;kurikulum&quot;: [
            {
                &quot;id&quot;: &quot;6c9c9b1b-6188-4080-aa82-a12149388ee4&quot;,
                &quot;name&quot;: &quot;SI20222&quot;,
                &quot;id_semester&quot;: &quot;7f5ad13e-ac75-46fb-8ada-562acec52570&quot;,
                &quot;smt&quot;: &quot;2022-2023 Genap&quot;,
                &quot;id_prodi&quot;: &quot;d4f99a8c-45c3-435b-a3b6-470679bba3bd&quot;,
                &quot;jenjang&quot;: &quot;S1&quot;,
                &quot;prodi&quot;: &quot;Sistem Informasi&quot;,
                &quot;sks_wajib&quot;: 24,
                &quot;sks_pilihan&quot;: 22,
                &quot;total_sks&quot;: 46,
                &quot;total_sks_wajib&quot;: &quot;81&quot;,
                &quot;total_sks_pilihan&quot;: 0,
                &quot;total_sks_sertifikat&quot;: &quot;4&quot;
            }
        ],
        &quot;mk&quot;: [
            {
                &quot;id&quot;: &quot;2b2e4042-3ae7-4632-a6e6-ac37e096f7c1&quot;,
                &quot;id_kurikulum&quot;: &quot;6c9c9b1b-6188-4080-aa82-a12149388ee4&quot;,
                &quot;id_matkul&quot;: &quot;d826ab00-fcbf-47c9-95da-9d2a8d628bee&quot;,
                &quot;kode&quot;: &quot;MU0507&quot;,
                &quot;matakuliah&quot;: &quot;Bahasa indonesia&quot;,
                &quot;sks_tm&quot;: &quot;2&quot;,
                &quot;praktek&quot;: 0,
                &quot;sks_pl&quot;: &quot;0&quot;,
                &quot;sks_sims&quot;: &quot;0&quot;,
                &quot;semester&quot;: 2,
                &quot;wajib&quot;: &quot;Wajib&quot;,
                &quot;jumlah_sks_wajib&quot;: &quot;2&quot;,
                &quot;jumlah_sks_pilihan&quot;: 0,
                &quot;jumlah_sks_sertifikat&quot;: 0,
                &quot;created_by&quot;: &quot;Yunita Sulistiani(2023-01-12 09:12:13)&quot;,
                &quot;updated_by&quot;: &quot;Yunita Sulistiani(2023-01-12 09:12:13)&quot;
            },
            {
                &quot;id&quot;: &quot;28495057-40d3-4255-9da1-ecbcbae39a92&quot;,
                &quot;id_kurikulum&quot;: &quot;6c9c9b1b-6188-4080-aa82-a12149388ee4&quot;,
                &quot;id_matkul&quot;: &quot;63acd9d5-9873-4852-bf1a-3b7da133ffc8&quot;,
                &quot;kode&quot;: &quot;DK0511&quot;,
                &quot;matakuliah&quot;: &quot;Sistem operasi&quot;,
                &quot;sks_tm&quot;: &quot;3&quot;,
                &quot;praktek&quot;: 0,
                &quot;sks_pl&quot;: &quot;0&quot;,
                &quot;sks_sims&quot;: &quot;0&quot;,
                &quot;semester&quot;: 2,
                &quot;wajib&quot;: &quot;Wajib&quot;,
                &quot;jumlah_sks_wajib&quot;: &quot;3&quot;,
                &quot;jumlah_sks_pilihan&quot;: 0,
                &quot;jumlah_sks_sertifikat&quot;: 0,
                &quot;created_by&quot;: &quot;Yunita Sulistiani(2023-01-12 09:12:52)&quot;,
                &quot;updated_by&quot;: &quot;Yunita Sulistiani(2023-01-12 09:12:52)&quot;
            },
            {
                &quot;id&quot;: &quot;53dffdf1-793c-41c0-9d6e-6b4e09c6d421&quot;,
                &quot;id_kurikulum&quot;: &quot;6c9c9b1b-6188-4080-aa82-a12149388ee4&quot;,
                &quot;id_matkul&quot;: &quot;bf063878-ab8a-4aad-b686-007a4e21d1b1&quot;,
                &quot;kode&quot;: &quot;MU0501&quot;,
                &quot;matakuliah&quot;: &quot;Kewirausahaan&quot;,
                &quot;sks_tm&quot;: &quot;2&quot;,
                &quot;praktek&quot;: 0,
                &quot;sks_pl&quot;: &quot;0&quot;,
                &quot;sks_sims&quot;: &quot;0&quot;,
                &quot;semester&quot;: 2,
                &quot;wajib&quot;: &quot;Wajib&quot;,
                &quot;jumlah_sks_wajib&quot;: &quot;2&quot;,
                &quot;jumlah_sks_pilihan&quot;: 0,
                &quot;jumlah_sks_sertifikat&quot;: 0,
                &quot;created_by&quot;: &quot;Yunita Sulistiani(2023-01-12 09:13:17)&quot;,
                &quot;updated_by&quot;: &quot;Yunita Sulistiani(2023-01-12 09:13:17)&quot;
            }, ...

列表数据API在Flutter中未显示

答案1

得分: 1

我看到你的未来定义是 null 且从未初始化。

Future<List<Mk>>? futureMk;

而你在 FutureBuilder 中使用了 futureMk 作为一个 future。由于 futureMknullsnapshot.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&lt;List&lt;Mk&gt;&gt;? 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&lt;List&lt;Mk&gt;&gt;(
      future: getDetail(&lt;your_id_here&gt;), //&#128072; I guess you are intending to use this future here and not futureMk
      builder: (context, snapshot) {
        if (snapshot.hasData) {
           // ListView Here
          ...
       }}

huangapple
  • 本文由 发表于 2023年3月1日 10:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定