Flutter将变量从dropdownmenu传递到另一个类会得到错误的值

时间:2019-10-07 11:48:18

标签: flutter

我似乎无法从我的下拉菜单中将正确的变量值传递给另一个类。

我创建了一个具有值“ Worker 1”,“ Worker 2”和“ Worker 3”的下拉菜单,并且我知道该下拉菜单有效,因为无法打印正确的值。

Here is when i create the dropdownbutton

    List<String> _dropdownValues = [

    ];
    for (var worker in workers) {
         _dropdownValues.add(worker.name);

      }
      String _selectedValue ="";
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          actions: <Widget>[
            DropdownButton<String>(
                    items: _dropdownValues.map((data)=>DropdownMenuItem<String>(
                      child: Text(data),
                      value: data,
                    )).toList(),
                    onChanged: (String value){
                      setState(() { 
                       _selectedValue = value; 

                      });
                    },
                    hint: Text('Assign checklist \n to worker'),
                  ),


Here i pass values to the second class Tasktile()

    body: TabBarView(
          children: <Widget>[
            Column(
              children: <Widget>[
                for (Task task in remainingTasks)
                  TaskTile(
                    title: task.title,
                    subtitle: task.subtitle,
                    checked: task.checked,
                    checklistId: widget.checklistId,
                    taskId: task.id,
                    tasks: task,
                    hidden: task.hidden,
                    selectedvalue: _selectedValue,

                  )
              ],
            ),

这里我在Tasktile()中检索值

class TaskTile extends StatefulWidget {
  final String title;
  final String subtitle;
  final bool checked;
  final int checklistId;
  final int taskId;
  final Task tasks;
  final bool hidden;
  final String selectedvalue;

  const TaskTile({
    Key key,
    @required this.title,
    @required this.subtitle,
    @required this.checked,
    @required this.checklistId,
    @required this.taskId,
    @required this.tasks,
    @required this.hidden,
    @required this.selectedvalue,

  }) : super(key: key);

但是当我尝试在这样的文本小部件中写入变量时:

Text('${widget.selectedvalue}')

并尝试打印/使用它使用_selectedvalue =“”的首次初始化,因此我假设TaskTile类无法从set state(){}获得_selectedvalue =值

0 个答案:

没有答案