George Maier
George Maier

Reputation: 25

Flutter - Too many positional arguments

I'm wanting to pass document and user objects to a dialogue box, coded in a separate document, which can perform an action on that users' document.

The code which passes over the information is:

 Widget _buildFunctionCards(BuildContext context, DocumentSnapshot document, FirebaseUser user) {
  return ListTile(
      title: GestureDetector( 
        onTap: () {
                      showDialog(
                        context: context,
                        builder: (_) => FunctionEditOptions(document, user),
                      );},
        child:Container... ()))}

The receiving document is coded thusly:

   class FunctionEditOptions extends StatefulWidget {
  FunctionEditOptions({this.db, this.user});
  final FirebaseUser user; 
  final DocumentSnapshot db;
  @override
  State<StatefulWidget> createState() => FunctionEditOptionsState();
}

class FunctionEditOptionsState extends State<FunctionEditOptions>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation<double> scaleAnimation;

  @override

The error message received is: "Too many positional arguments: 0 expected, but 2 found."

If anyone can provide an insight i'd be very grateful!

Upvotes: 1

Views: 1567

Answers (1)

aldobaie
aldobaie

Reputation: 1407

Change this:

builder: (_) => FunctionEditOptions(document, user),

to this

builder: (_) => FunctionEditOptions(db: document, user: user), // or:
builder: (_) => FunctionEditOptions(user: user, db: document), // both ways are correct

When specifying the parameters inside parentheses they become optional and maybe out of order. So you have to name the parameter when passing arguments.

Another way is to take out the parameters from the parentheses and make sure the arguments are in order.

FunctionEditOptions(this.db, this.user);

and call it this way:

builder: (_) => FunctionEditOptions(document, user),

You can mix both

FunctionEditOptions(this.db, {this.user});

and call it this way:

builder: (_) => FunctionEditOptions(document, user: user),

Upvotes: 3

Related Questions