【问题标题】:basic flutter question regarding the use of $ in flutter关于在颤振中使用 $ 的基本颤振问题
【发布时间】:2021-08-08 06:47:53
【问题描述】:

假设我已经声明了这样的表达式

bool Smp0 = false;
bool Smp1 = false;
bool Smp2 = false;

然后我使用组按钮小部件(内置复选框)

onSelected: (index, IsSelected){print('$index dan $IsSelected');}),

其中 index 是 0-2 之间的列表,IsSelected 是 true 和 false 之间的布尔值(与复选框相关联)。

现在我想将该索引与关联的 IsSelected 我的想法放在一起

Smp$index = $IsSelected;

但问题是这个表达式不起作用。我仍然不习惯记住一些 dart/flutter 代码。所以任何人都可以帮助我吗?

【问题讨论】:

  • 不能动态生成变量名。改用Listbools(例如var Smp = <bool>[]; 和后来的Smp[index] = IsSelected)。 $ 用于字符串插值,仅在字符串文字中才有意义。
  • 虽然这不是一个完全正确的答案,但它确实帮助我创建了一个数组,这样我就可以把我的变量提交给我的 back4app。所以谢谢男人

标签: flutter dart boolean


【解决方案1】:

所以是的。正如詹姆斯所说。我用这个制作了自己的数组。

class CheckBoxSetting {
  int index;
  String title;
  bool value;

  CheckBoxSetting({
    this.index,
    this.title,
    this.value,
  });
}

与此有关的内容

final notification = [
    CheckBoxSetting(title: 'Kertas', index: 0, value: false),
    CheckBoxSetting(title: 'Kaca', index: 1, value: false),
    CheckBoxSetting(title: 'Elektronik', index: 2, value: false),
    CheckBoxSetting(title: 'BesiLogam', index: 3, value: false),
    CheckBoxSetting(title: 'Plastik', index: 4, value: false),
    CheckBoxSetting(title: 'Alumunium', index: 5, value: false),
    CheckBoxSetting(title: 'Fabric', index: 6, value: false),
    CheckBoxSetting(title: 'Kayu', index: 7, value: false),
    CheckBoxSetting(title: 'Bio', index: 8, value: false),
    CheckBoxSetting(title: 'Mix', index: 9, value: true),
  ];

最后在组按钮中我做了这个(不是我声明了几个表达式,因为我需要那个表达式才能在 back4app 中使用)

onSelected: (index, IsSelected) async {
                              notification[index].value = IsSelected;
                              if (A0 == notification[index].title){
                                Ab0 = notification[index].value;
                              };
                              if (A1 == notification[index].title){
                                Ab1 = notification[index].value;
                              };
                              if (A2 == notification[index].title){
                                Ab2 = notification[index].value;
                              };
                              if (A3 == notification[index].title){
                                Ab3 = notification[index].value;
                              };
                              if (A4 == notification[index].title){
                                Ab4 = notification[index].value;
                              };
                              if (A5 == notification[index].title){
                                Ab5 = notification[index].value;
                              };
                              if (A6 == notification[index].title){
                                Ab6 = notification[index].value;
                              };
                              if (A7 == notification[index].title){
                                Ab7 = notification[index].value;
                              };
                              if (A8 == notification[index].title){
                                Ab8 = notification[index].value;
                              };
                              if (A9 == notification[index].title){
                                Ab9 = notification[index].value;
                              };
                            }),

A0-9 和 Ab0-9,我知道这是一个很长的代码,但它确实有效,这是最重要的。感谢@jamesdlin 的想法

【讨论】:

    猜你喜欢
    • 2021-02-01
    • 2021-11-16
    • 2021-05-05
    • 2022-01-12
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2021-12-16
    相关资源
    最近更新 更多