简体   繁体   中英

How to add dropdown button in flutter using getx?

How to fix this error? Here is my data.table.dart:

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(),
                )
                ),
              ),
            ])
          ]),
        ),
      )
    ],
  );
}
final selected = "".obs;

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

here is what I got in my console:

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) {
                                      ^^^^^^^^

Please help me this how to fix this error, THANKS!

There is two methods to solve your problem first just add.toString() after newValue like below code

   onChanged: ( newValue) {
                controller.setSelected(newValue.toString());
              },

Or you can just define the type in onChanged method like

 onChanged: (String? newValue) {
                controller.setSelected(newValue??'');
              },

I think This will help you. if you have any confusion feel free to ask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM