Home > Software design >  Flutter 3 positional argument(s) expected, but 2 found. Try adding the missing arguments
Flutter 3 positional argument(s) expected, but 2 found. Try adding the missing arguments

Time:09-28

I am using this model:

class ElementTask {
  final String name;
  final bool isDone;
  ElementTask(this.name, this.isDone);
}

Now I want to add:

final int frequency;

But then I get this error in my other class:

3 positional argument(s) expected, but 2 found. Try adding the missing arguments.

I am using this code:

 getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {
    List<ElementTask> listElement = [];
    int nbIsDone = 0;

    if (widget.user.uid.isNotEmpty) {
      // ignore: missing_return
      snapshot.data.documents.map<Column>((f) {
        if (f.documentID == widget.currentList.keys.elementAt(widget.i)) {
          f.data.forEach((a, b) { //<--error here**
            if (b.runtimeType == bool) {
              listElement.add(ElementTask(a, b));
            }
          });
        }
      }).toList();

      for (var i in listElement) {
        if (i.isDone) {
          nbIsDone  ;
        }
      }

What can I do to solve it? Should I add a c or something else?

CodePudding user response:

If you like to make frequency optional, make it nullable. and use named argument.

class ElementTask {
  final String name;
  final bool isDone;
  final int? frequency;
  ElementTask(this.name, this.isDone,{this.frequency});
}

If you like to have it required, you need to pass three item.

More about constructors

  • Related