首页 > 解决方案 > 颤振将变量从下拉菜单传递到另一个类得到错误的值

问题描述

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

我创建了一个下拉菜单,其值为“工人 1”、“工人 2”和“工人 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 = value

标签: flutter

解决方案


推荐阅读