0

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});
simi
  • 323
  • 4
  • 12

2 Answers2

0

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.

Ajil O.
  • 6,562
  • 5
  • 40
  • 72
0

This would work perfectly. Check it out.

   // declare a body construction object
   BodyConstruction _bodyConstruction;

   // initialise the object declared above
   _bodyConstruction = BodyConstruction(value: 'Your Value');

Note: When {} is used in a constructor or method body, it denotes a named parameter. Check the official documentation for more explanation.

Click here to read more on named parameters in Flutter

void
  • 12,787
  • 3
  • 28
  • 42