Search code examples
stringflutterdartstatefulwidget

Copy a value to another StatefulWidget


I tried to rebuild the code of this video (https://www.youtube.com/watch?v=d5PpeNb-dOY) , which is about how to copy a String value to another StatefulWidget, but now I get the BodyConstructionState(value) red underlined and it says "Too many positional arguments: 0 expected, but 1 found." Value comes from another widget. I don´t know why because I did it in the same way like the guy in the video did it. Can anybody tell me what my mistake is? Thanks in advance!

class BodyConstruction extends StatefulWidget {

      String value;
      BodyConstruction({Key key, @required this.value}) : super(key : key);


      @override
      _BodyConstructionState createState() => _BodyConstructionState(value);
    }

    class _BodyConstructionState extends State<BodyConstruction> {

      String value;
      _BodyConstructionState({this.value});

Solution

  • When you are creating an object of BodyContruction, you are supposed to do this

    BodyConstruction object = new BodyConstruction(value: "Some Value");
    

    and not

    BodyConstruction object = new BodyConstruction("Some Value");
    

    Creating a constructor parameter with {} makes them named optional parameters.

    Note: new keyboard is optional in Dart.