flutter - 颤振将变量从下拉菜单传递到另一个类得到错误的值
问题描述
我似乎无法将正确的变量值从我的下拉菜单传递给另一个类。
我创建了一个下拉菜单,其值为“工人 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
解决方案
推荐阅读
- c++ - 将 C++ 对象传递给 ChaiScript 函数
- typescript - 返回笑话测试的打字稿函数 - 返回类型应该是什么
- swift - 试图在应用程序运行时快速更改 UIView 的框架?
- ada - Ada 2012 RM - 注释和字符串文字
- javascript - IntersectionObserver 是否支持水平滚动观察?
- python-3.x - 这个赋值背后的逻辑是什么:了解numpy中的就地赋值操作
- javascript - 两个 else-if 语句导致 javascript 中的错误
- android - Android:具有双向绑定的复合或自定义视图
- json - Apache Spark (Scala):如何从 JSON RDD 中获取单个元素和子元素并将其存储在新的 RDD 中?
- c - 修改整数时C程序崩溃