以下是所有代码,我尝试了很多方法
我想替换磁贴的第一个id,然后
这是按编辑时弹出的窗口
这是答案
update
按下按钮的代码
onPressed: () async {
if (type == PopupType.add) {
saveThasbeeh(ctx, thasbeehController, countController);
} else {
//code to update
updateThasbeeh(id, data.thasbeeh, data.count, thasbeehController, countController, ctx);
}
},
类代码
@HiveType(typeId: 0)
class ThasbeehCategoryModel {
@HiveField(0)
final thasbeeh;
@HiveField(1)
final count;
@HiveField(2)
String? id;
ThasbeehCategoryModel({
required this.thasbeeh,
required this.count,
}) {
id = DateTime.now().millisecondsSinceEpoch.toString();
}
}
UpdateHasbeeh函数代码
updateThasbeeh(
id, thasbeeh, count, thasbeehController, countController, ctx) async {
final thasbeeh = thasbeehController.text;
final count = countController.text;
final updatedThasbeeh =
ThasbeehCategoryModel(count: count, thasbeeh: thasbeeh);
final _thasbeeh = await ThasbeehDB.instance.updateThasbeeh(updatedThasbeeh);
if (_thasbeeh == null) {
return;
} else {
Navigator.of(ctx).pop();
}
}
这是泰斯比。实例,更新函数代码
Future<ThasbeehCategoryModel?> updateThasbeeh(
ThasbeehCategoryModel data) async {
final _thasbeehDB = await Hive.openBox<ThasbeehCategoryModel>(box);
if (_thasbeehDB.values == null) {
return null;
}
//code to replace thru ID in database
thasbeehAddNotifer.value.clear();
refreshState();
thasbeehAddNotifer.notifyListeners();
return data;
}
以下是完整代码:
https://github.com/Mishalhaneef/counterappcode