Emir Bolat
Emir Bolat

Reputation: 1049

The named parameter 'onChanged' is required, but there's no corresponding argument. Try adding the required argument

My trainer writes code like this:

enter image description here

I am writing the same code but I am getting an error. The error is as follows:

enter image description here

What is the problem? How can I solve it? My codes:

import 'package:flutter/material.dart';

class dropDownMenu extends StatefulWidget {
  dropDownMenu({Key? key}) : super(key: key);

  @override
  State<dropDownMenu> createState() => _dropDownMenuState();
}

class _dropDownMenuState extends State<dropDownMenu> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: DropdownButton<String>(
        items: [
          DropdownMenuItem( child: Text('One'), value: 'One', ),
          DropdownMenuItem(child: Text('Two'), value: 'Two', ),
        ],
      ),
    );
  }
}

Error:

The named parameter 'onChanged' is required, but there's no corresponding argument.
Try adding the required argument.

Upvotes: 0

Views: 1812

Answers (2)

reza
reza

Reputation: 1508

onChanged parameter is required for the DropdownButton widget. it is called when the user selects an item. try this one:

class dropDownMenu extends StatefulWidget {
  dropDownMenu({Key? key}) : super(key: key);

  @override
  State<dropDownMenu> createState() => _dropDownMenuState();
}

class _dropDownMenuState extends State<dropDownMenu> {
  String value = 'One';
  @override
  Widget build(BuildContext context) {
    return Center(
      child: DropdownButton<String>(
        value: value,
        onChanged: (String? newValue) {
          setState(() {
            value = newValue!;
          });
        },
        items: [
          DropdownMenuItem(
            child: Text('One'),
            value: 'One',
          ),
          DropdownMenuItem(
            child: Text('Two'),
            value: 'Two',
          ),
        ],
      ),
    );
  }
}

Upvotes: 1

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63614

As the message says, onChanged is a required callback method that is needed to be implemented on DropdownButton

Follow the error message and do like

 child: DropdownButton<String>(
         onChanged: (value){ 
            //do your operation while chaning value
              }
         items: [
          DropdownMenuItem( child: Text('One'), value: 'One', ),
          DropdownMenuItem(child: Text('Two'), value: 'Two', ),
        ],
      ),

You can follow DropdownButton document.

Upvotes: 2

Related Questions