Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to widgets in Flutter

Tags:

flutter

dart

I am opening a modal dialog in Flutter and wish to pass in a single parameter (postId) to the modal for further processing. But this is generating an error as shown.

class SharingDialog extends StatefulWidget {
 @override

 final String postId;  // <--- generates the error, "Field doesn't override an inherited getter or setter"
 SharingDialog({
   String postId
 }): this.postId = postId;

 SharingDialogState createState() => new SharingDialogState(postId);
}

class SharingDialogState extends State<SharingDialog> {

 SharingDialogState(this.postId);
 final String postId;

 @override
 Widget build(BuildContext context) {
   return new Scaffold(
     appBar: 
       child: AppBar(           
         title: const Text('Share this Post'),
      actions: [
        new FlatButton(
          onPressed: () {
            print("Sharing Post ID: " + this.postId);
          },
          child: new Text('SHARE)
        ),
      ],
    ),
  ),
  body: new Text("SHARING SCREEN"),
);
}

And then there is a click to open the modal using the following code which generates the accompanying error:

Code:

return new SharingDialog(postId);

Error: Too many positional arguments: 0 allowed, but 1 found.

How do you pass parameter(s) if not this way?

like image 809
lilbiscuit Avatar asked Dec 06 '18 20:12

lilbiscuit


1 Answers

First:

Remove override keyword above postId

  @override <-- this one
  final String postId;

Second:

Because you are using named parameter , send the param like this way:

   return new SharingDialog(postId: postId);

If you want more information about Optional named parameters, check this link:

https://www.dartlang.org/guides/language/language-tour#optional-parameters

like image 156
diegoveloper Avatar answered Oct 11 '22 01:10

diegoveloper