用飞镖条件设置变量

时间:2020-02-04 15:12:14

标签: flutter dart flutter-layout dart-polymer flutter-animation

我有select a, b from table where c is NULL group by a, b having count(*)>1; 类,它有一个变量MyDateTime。创建对象时,我需要在某种条件下设置此变量。例如,我有这个对象:
hour

现在我需要增加MyDateTime dt = MyDateTime(2020, 2, 3, 3, 2);hour

我的问题是如何在不添加新功能的情况下更改对象的dt.hour++;,同时我需要在有条件的情况下增加hour

hour

我不想使用功能(例如: class MyDateTime { int year; int month; int day; int hour; int minute; int second; MyDateTime({this.year, this.month, this.day ,this.hour=0, this.minute=0, this.second=0}); // this is the condition set addHour(int h){ if(this.hour == 23) this.hour = 0; else if(this.hour == 0) this.hour = 1; else this.hour++; } }

有办法吗?

2 个答案:

答案 0 :(得分:2)

您可以为此使用自定义设置器:

sendMessage

然后您可以执行以下操作:

class MyDateTime {
  int year;
  int month;
  int day;
  int _hour;
  int minute;
  int second;

  MyDateTime({this.year, this.month, this.day, int hour=0, this.minute=0, this.second=0}) : _hour = hour;


  // this is the condition
  set hour(int h) => _hour = h % 24;

  // We need to define a custom getter as well.
  int get hour => _hour;
}

答案 1 :(得分:0)

  1. 对于自动递增:
    查看Timer,它将使您在定义的hour之后自动递增Duration
    https://fluttermaster.com/tips-to-use-timer-in-dart-and-flutter/
  2. 要为您的增量增加条件:
    如果不想为了增加hour变量而继续调用函数,则必须在hour变量中添加某种侦听器,请看一下以下包装:

    property_change_notifier: https://pub.dev/packages/property_change_notifier

    hour中添加侦听器将帮助您定义一个函数,例如addHour()函数,该函数将在hour的值更改时自动调用。

    或您可以在Timer本身内添加递增条件。