【问题标题】:How to create dropown list in flutter where data comes from API?如何在数据来自 API 的颤动中创建下拉列表?
【发布时间】:2021-09-25 19:03:41
【问题描述】:

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

【问题讨论】:

标签: flutter dart flutter-layout


【解决方案1】:

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

  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 函数,网址如下:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    相关资源
    最近更新 更多