skip to Main Content

I am trying to build a dropdownbutton in flutter, but I am getting an error

type ‘String’ is not a subtype of type ‘MorphShape’ of ‘function result’

I have a class:

class MorphShape {
  Shape value;
  String name;
  MorphShape(this.value, this.name);
}

I init a list of possible values for the dropdown

  final List<MorphShape> morphShapes = [
    MorphShape(Shape.rect, 'rect'),
    MorphShape(Shape.cross, 'cross'),
    MorphShape(Shape.ellipse, 'ellipse')
  ];
  late MorphShape morphKernelShape = morphShapes[2];

and finally setup the dropdown

                    Center(
                        child: Padding(
                      padding: const EdgeInsets.fromLTRB(0, 0, 0, 25),
                      child: DropdownButton(
                        value: morphKernelShape,
                        onChanged: (MorphShape? morphShape) {
                          setState(() {
                            morphKernelShape = morphShape!;
                          });
                        },
                        items: morphShapes.map<DropdownMenuItem<MorphShape>>(
                            (MorphShape value) {
                          return DropdownMenuItem(
                              value: value, child: Text(value.name));
                        }).toList(),
                      ),
                    )),

The IDE itself doesn’t highlight anything as a problem, but when I try to run my app it gives me the above stated error. I can’t seem to figure out what is the problem here?

2

Answers


  1. Chosen as BEST ANSWER

    The code works, if anyone else runs into a similar issue, it most likely is due to hot reload.


  2. Here you’re passing the MorphShape object to the value which accepts String :

     value: morphKernelShape, // this should be String
         onChanged: (MorphShape? morphShape) {
              setState(() {
                 morphKernelShape = morphShape!; // here you're passing to it MorphShape object.
              });
     // ...
    

    maybe you wanted to do this instead:

    value: morphKernelShape.name,
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search