class - 以下方法怎么可能使用为构造函数保留的“语法糖”?
问题描述
为了说明我的观点,以下代码包含一个名为 ColorValueChanger 的方法,它使用 this.passedIn 作为可选参数。我以为这是为构造函数保留的?
class Foo extends StatefulWidget {
final String passedIn;
// Value passed in from its host
ColorValueChanger({Key key, this.passedIn}) : super(key: key);
_FooState createState() => new _FooState();
}
class _FooState extends State<Foo> {
@override
Widget build(BuildContext context) {
return Text(widget.passedIn,);
}
}
解决方案
它是一个构造函数。听起来更像是示例中有错字。
固定代码是:
class Foo extends StatefulWidget {
final String passedIn;
// Value passed in from its host
Foo({Key key, this.passedIn}) : super(key: key);
_FooState createState() => new _FooState();
}
class _FooState extends State<Foo> {
@override
Widget build(BuildContext context) {
return Text(widget.passedIn,);
}
}
推荐阅读
- excel - 自动过滤可见细胞的 VLOOKUP
- arrays - Excel Match Multiple Criteria Lookup Array:从列表中查找包含所有值的列,并返回列在数组中的位置
- web-scraping - scrapy文件管道下载文件并覆盖它
- javascript - 如何将 Array.prototype.filter 与对象一起使用
- amazon-web-services - 推动放大环境后的 grequests 问题
- reactjs - 在 React 上刷新后保持在同一个 TabPanel 上
- typescript - 在 TypeScript 中使用 MemoryDescriptor(用于 new WebAssembly.Memory())
- python - 如何保存 AzureML Pipelines 的输出
- python - 当键指向数组时遍历字典键?
- javascript - 如何确保类型检查函数为相同的参数返回相同的引用