Replies: 2 comments 2 replies
-
The above is an abstract of the issue I encountered. The problem I faced is as follows: I am using GetX. I loop through a list and create different controllers based on habit.id. Widget habitList(context) {
return ListView.builder(
padding: const EdgeInsets.only(top: 0),
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: controller.habitDataController.habits.length,
itemBuilder: (context, index) {
Habit habit = controller.habitDataController.habits[index];
final habitController =
Get.isRegistered<HabitItemController>(tag: habit.id.toString())
? Get.find<HabitItemController>(tag: habit.id.toString())
: Get.put(HabitItemController(habit),
tag: habit.id.toString(), permanent: true);
return GestureDetector(
onTap: () {
controller.homeController.onClickHabit(habit);
},
child: HabitItemView(
habitController: habitController,
),
);
},
);
}
In each controller, I called the listeningHabit() method as mentioned above. Assuming I have now created 4 controllers, there are now 4 watchAllHabitCheckByHabitIdWithYear instances, and their query conditions habitId are different. static recordHabitCheck(int habitId,
{required DateTime targetDate,
required DateTime dateTime,
completionPerDay = 1}) async {
return db.transaction(() async {
final existingCheck = await (db.select(db.habitChecksTable)
..where((check) => check.habitId.equals(habitId))
..where((check) => check.targetDate.equals(targetDate)))
.getSingleOrNull();
logUtil("existingCheck ${existingCheck?.toJson()}");
if (existingCheck == null) {
await db
.into(db.habitChecksTable)
.insert(HabitChecksTableCompanion.insert(
habitId: Value(habitId),
numberOfCompleted: const Value(1),
completionPerDay: Value(completionPerDay),
completionTime: completionPerDay == 1
? Value(dateTime)
: const Value.absent(),
updateAt: DateTime.now(),
targetDate: Value(targetDate),
));
} else {
if (existingCheck.numberOfCompleted < existingCheck.completionPerDay) {
int currentNumberOfCompleted = existingCheck.numberOfCompleted + 1;
await (db.update(db.habitChecksTable)
..where(
(habitCheck) => habitCheck.id.equals(existingCheck.id!)))
.write(
HabitChecksTableCompanion(
id: Value(existingCheck.id),
numberOfCompleted: Value(currentNumberOfCompleted),
updateAt: Value(DateTime.now()), // 更新修改时间
completionTime:
currentNumberOfCompleted == existingCheck.completionPerDay
? Value(dateTime)
: const Value.absent(),
),
);
} else {
await (db.update(db.habitChecksTable)
..where(
(habitCheck) => habitCheck.id.equals(existingCheck.id!)))
.write(
HabitChecksTableCompanion(
id: Value(existingCheck.id),
numberOfCompleted: Value(0),
updateAt: Value(DateTime.now()), // 更新修改时间
completionTime: Value<DateTime?>(null),
),
);
}
}
});
}
Now when I call the above method to add or update data, I find that all four existing streams are triggered. |
Beta Was this translation helpful? Give feedback.
-
All four streams will trigger - you'd have to use |
Beta Was this translation helpful? Give feedback.
-
The above is one of my query related methods.
I called listeningHabit() at different locations in the code 4 times, and each time the habit.value.id! is different, for example, they are 1, 2, 3, and 4 respectively.
So when I update or insert data into the database, suppose there is a data id 3 habitId 2. Then when this data is inserted into the database. Will all 4 streams trigger or only the habit.value.id! Is that stream trigger for 2?
Beta Was this translation helpful? Give feedback.
All reactions