ArthurEKing
ArthurEKing

Reputation: 158

1 positional argument(s) expected (Flutter, ios)

I'm sure this is going to be a simple and easy one, but I'm still too new, and have zero clue as to how to resolve.

I was having an issue with my program, in that I needed to run a method/function in my class, but couldn't merely add the class that contains it as a final variable into the class that needed it, because I couldn't be certain that the particular copy of the class that needed it would be active in the hierarchy.

So my solution (Thanks to Stack Overflow) was to separate the logic out, thanks to the suggestion on How to call method from another class in Flutter(Dart)?

There proved to be a few errors though in trying to implement it.

I was able to separate off the functions/methods into a separate logic class, and add the logic class alone as a final, not a problem.

going from:

class MyClass extends StatefulWidget {
  // Random code that is unimportant to the question
  void myMethod(){}

}

to:

class Logic {

void myMethod(){}
}

class MyClass extends StatefulWidget {
  final Logic logic;
  MyClass(this.logic);
  // Random code that is unimportant to the question
  
}

and using this.myMethod(); to call the function is perfectly fine, no issues.

My issue however, is that there are several times in the program where I'm creating an instance of MyClass(); and it's apparently missing a "positional argument" now, where it didn't need it previously. I tried adding a positional argument for Logic: but that wasn't an available option, so... what is needed? I know I just need to pass something in through to them, but I don't know what.

Upvotes: 0

Views: 239

Answers (1)

rmtmckenzie
rmtmckenzie

Reputation: 40433

Well I can see how the error might be confusing because dart's syntax is a bit different than Java's and other common languages for the constructor. When you define the constructor in MyClass (i.e. MyClass(this.logic)) you've told to expect an instance of the Logic class to be passed in, in the first position.

So going from your example:

class Logic {
  void myMethod(){}

  // in dart, classes have an automatic zero-argument constructor
}

class MyClass extends StatefulWidget {
  final Logic logic;
  MyClass(this.logic);
  // Random code that is unimportant to the question
}

To use it, you'd need to do something like this:

void main() {
  final logic = Logic();
  final myClass = MyClass(logic);

  // do whatever with myClass
}

The constructor is equivalent to this:

class MyClass extends StatefulWidget {
  final Logic logic;
  MyClass(Logic logic): logic = logic;
}

For a bit of background, if you used an optional positional argument it wouldn't require logic, and logic would default to null:

class MyClass extends StatefulWidget {
  final Logic logic;
  MyClass([this.logic]);
}

void main() {
  final myClass = MyClass();
}

And similarly if you used a named argument it wouldn't require it either, but if you did want to pass it in, you'd have to specify the name:

class MyClass extends StatefulWidget {
  final Logic logic;
  MyClass({this.logic});
}

void main() {
  final myClass = MyClass(logic: Logic());
}

If you're having trouble with dart-specific things, I'd highly advise giving the java-to-dart codelab a go as it explains a lot of the syntactic differences between traditional languages like java/c#, and c++ to an extent (tbh dart borrows a bit from each).

Upvotes: 2

Related Questions