我在摆弄GlobalObjectKey
,我的对象是DateTime
实例。我注意到,如果我创建相同日期和时间的新实例,我会得到相同的实例“ID”:
DateTime newInstance = new DateTime.fromMicrosecondsSinceEpoch(datetime.microsecondsSinceEpoch);
我的调试打印说有两个具有相同 ID 的单独键:
[GlobalObjectKey DateTime#f056e] // Key assigned to Widget1
[GlobalObjectKey DateTime#f056e] // Key assigned to Widget2
但是,即使键似乎是重复的,我似乎也没有收到任何小部件/构建错误。
这是我正在做的更完整的示例:
class DateTimeKey{
GlobalObjectKey key;
DateTimeKey(DateTime datetime){
DateTime newInstance = new DateTime.fromMicrosecondsSinceEpoch(datetime.microsecondsSinceEpoch);
key = new GlobalObjectKey(newInstance);
}
}
...
List<DateTimeKey> _bookingListMonthKeys = [];
List<DateTimeKey> _bookingListDayKeys = [];
DateTimeKey _monthKey = new DateTimeKey(theDate);
_bookingListMonthKeys.add(_monthKey);
DateTimeKey _dayKey = new DateTimeKey(theDate); // theDate here refers to the same DateTime instance as above
_bookingListDayKeys.add(_dayKey);
即使我遍历两个列表并像这样交叉引用它们
_bookingListDayKeys.forEach((dayKey){
_bookingListMonthKeys.forEach((monthKey){
if( identical(dayKey, monthKey) )
print('Identical DateTimeKeys found: $dayKey, $monthKey');
if( identical(dayKey.key, monthKey.key) )
print('Identical GlobalObjectKeys found: ${dayKey.key}, ${monthKey.key}');
});
});
它没有显示任何重复,但上面的打印输出显然具有相同的“id”(#f056e
)。有人可以解释这里发生了什么吗?