简体   繁体   English

Flutter - 太多的位置 arguments

[英]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."收到的错误消息是:“位置 arguments 太多:预期为 0,但找到了 2。”

If anyone can provide an insight i'd be very grateful!如果有人能提供见解,我将不胜感激!

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.所以在传递arguments时必须给参数命名。

Another way is to take out the parameters from the parentheses and make sure the arguments are in order.另一种方法是从括号中取出参数并确保 arguments 是有序的。

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),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM