Dart:Dart 如何匹配类的构造函数中的命名参数?

标签 dart flutter

Dart 如何匹配类的构造函数中的命名参数?

示例(有效):

Class MyWidget {

    final String a;
    final String b;

    MyWidget (
        @required this.a,
        @required this.b
    )

    @override // Yes, it's Flutter
    Widget build(BuildContext context) {
        return ....
    }
}


/// Calling MyWidget
return MyWidget(
    a: x,
    b: y
)

这按预期工作。 但在此设置中,我被迫将 MyWidget 中的变量命名为与命名参数相同的名称,因为调用中的“a”与 MyWidget 中的“this.a”相同。

我想要的是这样的:

Class MyWidget {

   final String aaa;
   final String bbb;

   MyWidget (
       @required a // And assign that value to this.aaa,
       @required b // And assign that value to this.bbb
   )
}

如何将传递的命名参数“a”的值分配给局部变量“aaa”?

最佳答案

你必须像这样权衡 this.xxx 语法的简单性:

class MyWidget {
  final String aaa;
  final String bbb;

  MyWidget({a, b})
      : aaa = a,
        bbb = b;
}

关于Dart:Dart 如何匹配类的构造函数中的命名参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55548548/

相关文章:

facebook - 如何确保方法等待 http 响应而不是在 Dart 中返回 null?

debugging - Dart调试器及时调用方法

flutter - 如何在Flutter上实现Listview设计

firebase - 在 Xcode 中存档时 Flutter Firestore 插件抛出错误

flutter - 如何在 flutter 中安排边框布局?

flutter : How to add a Header Row to a ListView

flutter - BLE 模块问题 : Exception: Another scan is already in progress

dart - angular2dart组件的属性和方法

dart - 无法在错误处理程序中访问属性

flutter - 耀斑和 flutter 问题