我要在Flutter中打开一个模式对话框,并希望将单个参数(postId)传递给该模式进行进一步处理。但这会生成一个错误,如下所示。
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"),
);
}
然后单击以使用以下代码打开模式,该代码会生成相应的错误:
代码:
return new SharingDialog(postId);
错误:Too many positional arguments: 0 allowed, but 1 found.
如果不是这样,如何传递参数?
发布于 2018-12-07 05:00:10
首先:
删除postId上的覆盖关键字
@override <-- this one
final String postId;
第二:
因为您使用的是命名参数,所以像这样发送参数:
return new SharingDialog(postId: postId);
如果需要有关可选命名参数的更多信息,请查看此链接:
https://www.dartlang.org/guides/language/language-tour#optional-parameters
https://stackoverflow.com/questions/53659570
复制相似问题