如何在数据来自 API 的颤动中创建下拉列表? [英] How to create dropown list in flutter where data comes from API?

查看:12
本文介绍了如何在数据来自 API 的颤动中创建下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在下拉列表中显示教师姓名列表,当我单击教师姓名时,它需要转到该教师的相应作业.我是新手,我该怎么做?

I need to display list of teachers name in the dropdown list and when I click teacher name it need to go to respective homework of that teacher. I am new to flutter how can I do this?

推荐答案

你应该尝试这样创建你自己的两个 API 1. 选择所有教师 2. 获取所选用户的主题.

You should try to this create your own tow API's 1. Select All Teachers 2. Get the subject of selected user.

  String sid;
  List data = List();
  String url = "https://example.com/teacher";
  //your all teacher api call
  Future fetchTeacher() async {
    var result = await http.get(url, headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    });
    var jsonData = json.decode(result.body);

    setState(() {
     data = jsonData;
    });
   return jsonData;
 }
 // add your API call with ininState() function
 @override
 void initState() {
   super.initState();
   fetchTeacher();
  }
  // Your Widget for All teachers dropdown list 
   DropdownButtonHideUnderline(
        child: DropdownButton(
          value: sid,
          hint: Text("Select Stockiest",
              style: TextStyle(color: Colors.black)),
          items: data.map((list) {
            return DropdownMenuItem(
              child: Text(list['name']),
              value: list['sid'].toString(),
            );
          }).toList(),
          onChanged: (value) {
            setState(() {
              sid = value;
            });
          },
        ),
      ),

为选定的教师科目尝试相同的 API 函数,网址如下:

Try Same API function for selected teacher subject the url like this:

String url = "https://example.com/teacher"+sid;

为选定的教师传递上述 url 的 id 并将此 id 推送给您的主题小部件,您将获得选定的教师主题数据

pass the id for above url for selected teacher and push this id for the your subject widget you get the selected teacher subject data

这篇关于如何在数据来自 API 的颤动中创建下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆