首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用getx在颤振中添加下拉按钮?

如何使用getx在颤振中添加下拉按钮?
EN

Stack Overflow用户
提问于 2022-11-25 05:28:51
回答 1查看 33关注 0票数 0

如何修复此错误?以下是我的数据table.dart:

代码语言:javascript
运行
复制
Widget build(BuildContext context) {
  Controller controller = Get.put(Controller());
  return Column(
    children: [
      SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: DataTable(columns: [
            DataColumn(
              label: Text('Order No'),
            ),
            DataColumn(
              label: Text('Quantity'),
            ),
            DataColumn(
              label: Text('Retail Price'),
            ),
            DataColumn(
              label: Text('Payment Status'),
            ),
            DataColumn(
              label: Text('Date'),
            ),
            DataColumn(
              label: Text('Assign Rider'),
            ),
          ], rows: [
            DataRow(
                cells: [
              DataCell(Text('637c8e3a')),
              DataCell(Text('1')),
              DataCell(Text('1.00')),
              DataCell(Text('UnPaid')),
              DataCell(
                Text('2022-11-22 08:54:18'),
              ),
              DataCell(
                Obx( () => DropdownButton(
                  hint: Text(
                    'Book Type',
                  ),
                  onChanged: (newValue) {
                    controller.setSelected(newValue);
                  },
                  value: controller.selected.value,
                  items: controller.listType.map((selectedType) {
                    return DropdownMenuItem(
                      child: new Text(
                        selectedType,
                      ),
                      value: selectedType,
                    );
                  }).toList(),
                )
                ),
              ),
            ])
          ]),
        ),
      )
    ],
  );
}
代码语言:javascript
运行
复制
final selected = "".obs;

void setSelected(String value){
  selected.value = value;
}

,这是我在我的控制台中得到的:

代码语言:javascript
运行
复制
Performing hot reload...
Syncing files to device TECNO CH7n...
lib/widget/data_table.dart:52:46: Error: The argument type 'Object?' can't be assigned to the parameter type 'String'.
 - 'Object' is from 'dart:core'.
                      controller.setSelected(newValue);
                                             ^
lib/widget/data_table.dart:55:39: Error: The getter 'listType' isn't defined for the class 'Controller'.
 - 'Controller' is from 'package:vendor_app/controller/dropdown_controller.dart' ('lib/controller/dropdown_controller.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'listType'.
                    items: controller.listType.map((selectedType) {
                                      ^^^^^^^^

请帮我解决这个错误,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-11-26 02:35:17

有两种解决问题的方法,首先在.toString()后面添加newValue (),如下所示

代码语言:javascript
运行
复制
   onChanged: ( newValue) {
                controller.setSelected(newValue.toString());
              },

也可以在onChanged方法中定义类型,如下所示

代码语言:javascript
运行
复制
 onChanged: (String? newValue) {
                controller.setSelected(newValue??'');
              },

我想这会对你有帮助。如果你有任何困惑,请随便问

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74568653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档