Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

dart - How to create dropown list in flutter where data comes from 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
            });
          },
        ),
      ),

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...