Flutter 如何在颤振中更改下拉值时调用api?

Flutter 如何在颤振中更改下拉值时调用api?,flutter,dart,Flutter,Dart,在我的颤振应用程序中,我有两个下拉按钮,第一个是选择主题,第二个是选择讲座编号。如果我选择主题和讲座编号,则调用api studentList并在列表视图中显示学生列表 这是我的代码: class SubjectWiseAttendance extends StatefulWidget { final imei_no,school_code,employee_id; SubjectWiseAttendance({Key key, this.imei_no, this.

在我的颤振应用程序中,我有两个下拉按钮,第一个是选择主题,第二个是选择讲座编号。如果我选择主题和讲座编号,则调用api studentList并在列表视图中显示学生列表

这是我的代码:

    class SubjectWiseAttendance extends StatefulWidget {
    final imei_no,school_code,employee_id;
      SubjectWiseAttendance({Key key, this.imei_no, this.school_code, this.employee_id})
          : super(key: key);
      @override
      _SubjectWiseAttendanceState createState() => _SubjectWiseAttendanceState();
    }
    class _SubjectWiseAttendanceState extends State<SubjectWiseAttendance> {
      String date =DateFormat("dd-MM-yyyy").format(DateTime.now());
      String holder='9';
      String get imei_no => '0';
      String get school_code => 'STAGING';
      String get employee_id => 'EMP450';
      String get class_id=>holder;
      String get subject_id=>'0';
      String get attendance_date=>date;
      String get standard_id=>'0';
      String get attendance_type=>'S';
      String get hr_id=>'0';
      String get batch_id=>'0';
      String get sort_by_lect_no=>'0';
      String get Absent_Enroll_No =>new_str;
      String get proxy_flag =>'N';
      String get proxy_reason =>" ";
      String get late_flag=>'N';
      String get homeroom_id=>'0';
      String get lecture_id =>'0';
      List<dynamic> filteredItems;
      List<dynamic> filteredItemsType;
      String _selectedLocation;
      Future api1;
      var result ;
      var msg;
      var validate;
      var validate1;
      var api2;
      var array;
      @override
      void initState() {
        print('initState');
        _selectedLocation;
       _lectures;
       api1=_fetchClassdivision(imei_no, school_code, employee_id);
       api2=_fetchStudentlist(imei_no, school_code, employee_id, class_id, subject_id, attendance_date, standard_id, attendance_type, hr_id, batch_id, sort_by_lect_no);
        super.initState();
      }
      @override
      void dispose() {
        super.dispose();
      }
      _fetchClassdivision(
          String imei_no, String school_code, String employee_id) async {
        var apiUrl ='https://staging.ethdigitalcampus.com/T-View/service/DC/getClassDivisionSubjectwise' ;
        List<ClassDivisionSubject> list;
        Map<String, String> headers = {

          'content-type': 'application/x-www-form-urlencoded'
        };
        print('api1');
        Map<String, String> body = {
          'imei_no': imei_no,
          'school_code': school_code,
          'employee_id': employee_id,
        };
        http.Response response =
        await http.post(apiUrl, body: body, headers: headers);
        var data = json.decode(response.body);
        return data ;
       } 

      **List _buildFilteredList(List<dynamic> data) {
        List filteredItems = [];
        for(int j=0;j<data.length;j++)
        {
        String subject = data[j]['class_desc']+"--"+ data[j]['subject_desc'];
        filteredItems.add(subject.toString());
        }
        return filteredItems;
      }**
     _fetchStudentlist(...)
      Widget listViewWidget(data) {...
       }
    final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
     @override
      Widget build(BuildContext context) {
       body: new SafeArea(
              top: false,
              bottom: false,
              child: new Form(
                  key: _formKey,
                  autovalidate: true,
                  child: new ListView(
                   children: <Widget>[
                   Container(
                        child:FutureBuilder(
                            future:api1,
                            builder: (BuildContext context,snapshot) {
                              print('class division');
                              if (snapshot.data == null) {
                                return Column(children: <Widget>[
                                  Container(
                                      child: Center(
                                        child:  CircularProgressIndicator()
                                      ))
                                ]);
                              } else {
                                if(result =='0') {
                                  filteredItems = _buildFilteredList(array);
                                  if (filteredItems.length > 0) {
                                    if(_flag == true){
                                      _selectedLocation = filteredItems[0];
                                    }
                                     return Column(children: <Widget>[
                                      Divider(),
                                      DropdownButton(
                                       isExpanded: true,
                                        isDense: true,
                                        value: _selectedLocation,
                                        onChanged: (newValue) {
                                          setState(() {
                                            _flag = false;
                                            _selectedLocation = newValue
                                         });
                                        },
                                        items: filteredItems.map((subject) {
                                          return DropdownMenuItem(
                                            child: new Text(subject),
                                            value: subject,
                                          );
                                        }).toList(),
                                      ),
                                    ]
                                    );
                                  }
                                } }}),
                      ),
                       child: FutureBuilder(
                            future: api2,
                            builder: (context, snapshot) {
                              print('check student list');
                              if (snapshot.hasError) print(snapshot.error);
                              return snapshot.data != null
                                  ? listViewWidget(snapshot.data)
                                  : Center(child: CircularProgressIndicator());
                            }),
                      ),

                    ],
                  )
              )
          ),
 }
class subjectWiseAttention扩展StatefulWidget{
最终imei编号、学校代码、员工id;
SubjectWiseAttention({Key Key,this.imei\u no,this.school\u code,this.employee\u id})
:super(key:key);
@凌驾
_SubjectWiseAttendantEstate createState()=>_SubjectWiseAttendantEstate();
}
类_SubjectWiseAttendanceState扩展{
字符串date=DateFormat(“dd-MM-yyyy”).format(DateTime.now());
持弦器class='9';
字符串get imei_no=>“0”;
字符串get school_code=>“STAGING”;
字符串get employee_id=>EMP450;
字符串get class_id=>holder;
字符串get subject_id=>“0”;
字符串get Attention_date=>date;
字符串get standard_id=>'0';
字符串get attention_type=>'S';
字符串get hr_id=>“0”;
字符串get batch_id=>“0”;
字符串get sort_by_lect_no=>0;
字符串get-empture\u-Enroll\u-No=>new\u-str;
字符串get proxy_flag=>'N';
字符串get proxy_reason=>“”;
字符串get late_flag=>'N';
字符串get homeroom_id=>“0”;
字符串get touch_id=>'0';
列出过滤数据项;
列表filteredItemsType;
字符串\u选择的位置;
未来api1;
var结果;
var-msg;
var验证;
var验证1;
api2变种;
变量数组;
@凌驾
void initState(){
打印('initState');
_选择位置;
_讲座;
api1=_fetchClassdivision(imei编号、学校代码、员工id);
api2=\u fetchStudentlist(imei\u编号、学校代码、员工id、班级id、科目id、出勤日期、标准id、出勤类型、人力资源id、批次id、排序依据选择号);
super.initState();
}
@凌驾
无效处置(){
super.dispose();
}
_类划分(
字符串imei\u no、字符串school\u code、字符串employee\u id)异步{
var apiUrl=https://staging.ethdigitalcampus.com/T-View/service/DC/getClassDivisionSubjectwise' ;
名单;
映射头={
“内容类型”:“应用程序/x-www-form-urlencoded”
};
打印(“api1”);
地图正文={
“imei_no”:imei_no,
“学校代码”:学校代码,
“员工id”:员工id,
};
响应=
等待http.post(apirl,body:body,headers:headers);
var data=json.decode(response.body);
返回数据;
} 
**列表\u buildFilteredList(列表数据){
列表filteredItems=[];
对于(int j=0;j 0){
如果(_flag==true){
_selectedLocation=filteredItems[0];
}
返回列(子项:[
分隔符(),
下拉按钮(
是的,
是的,
值:_selectedLocation,
一旦更改:(newValue){
设置状态(){
_flag=false;
_selectedLocation=newValue
});
},
项目:filteredItems.map((主题){
返回下拉菜单项(
儿童:新文本(主题),
价值:主体,
);
}).toList(),
),
]
);
}
} }}),
),
孩子:未来建设者(
未来:api2,
生成器:(上下文,快照){
打印(“检查学生名单”);
if(snapshot.hasError)打印(snapshot.error);
返回snapshot.data!=null
?listViewWidget(snapshot.data)
:居中(子项:循环压缩机指示器());
}),
),
],
)
)
),
}
输出:


如果更改下拉列表值,那么如何根据下拉列表值更改studentList。

下拉列表的
onChanged
中,添加对调用api的方法的调用:

DropdownButton(
  ...
  onChanged: (String subject_id) {
    _fetchStudentlist(..., subject_id, ...);
  },
  ...
),

你好欢迎来到StackOverflow。您需要在共享的代码中更加简洁和具体。如果您的问题是下拉列表,请仅共享与之相关的代码。没有人会通过500行以上的代码来试图找到您需要帮助的代码行。检查指导原则:现在我更新我的问题。如果您有位置和讲座下拉列表,没有主题id下拉列表,我想将所选主题的主题id传递给_fetechStudentList()。而且,您仍然有太多的代码。要对这么多代码进行排序以找到几行重要的代码是非常困难的。如果您需要帮助,请遵循问题指南并改进您的问题:抱歉,问题格式。现在忘记了讲座下拉列表。更新代码只有一个下拉列表。