Home > Blockchain >  type 'String' is not a subtype of type 'MorphShape' of 'function result
type 'String' is not a subtype of type 'MorphShape' of 'function result

Time:11-29

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?

CodePudding user response:

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,

CodePudding user response:

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

  • Related