我是颤振中的对象框的新手,并且在尝试将对象放入商店时已经遇到错误。我有以下代码:
对象框类
import 'package:finsec/features/income/data/models/Income.dart';
import '../../../../objectbox.g.dart';
import 'dart:async';
class ObjectBox {
/// The Store of this app.
late final Store store;
late final Box<Income> incomeBox;
/// A stream of all notes ordered by date.
late final Stream<Query<Income>> incomeQueryStream;
ObjectBox._create(this.store) {
// Add any additional setup code, e.g. build queries.
incomeBox = Box<Income>(store);
final qBuilder = incomeBox.query(Income_.monthNumber.equals(1) & Income_.calendarYear.equals(2022));
incomeQueryStream = qBuilder.watch(triggerImmediately: true);
// Stream<Query<Income>> watchedQuery = incomeBox.query().watch();
}
/// Create an instance of ObjectBox to use throughout the app.
static Future<ObjectBox> create() async {
// Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
final store = await openStore();
return ObjectBox._create(store);
}
}
然后在我的 main.dart 文件中,我有以下内容
/// Provides access to the ObjectBox Store throughout the app.
late ObjectBox objectBox;
late SyncClient _syncClient;
bool hasBeenInitialized = false;
Future<void> main() async {
// This is required so ObjectBox can get the application directory
// to store the database in.
WidgetsFlutterBinding.ensureInitialized();
objectBox = await ObjectBox.create();
runApp(new MyHomePage( initialDate: DateTime.now()));
}
class MyHomePage extends StatefulWidget {
final DateTime initialDate;
const MyHomePage({required this.initialDate}) ;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
//some code here
}
在另一个名为 incomeModel.dart 的类中,我试图调用 putMany 函数。这是其他类的部分代码
void saveIncome(String status) {
final isoCalendar = IsoCalendar.fromDateTime(this.form.value[datePaidLabel]);
int groupID = Utilities.getUniqueCode();
List<Income> incomeList = <Income>[];
Income income = new Income(
groupID: groupID,
expectedAmount: double.parse(this.form.value[incomeAmountLabel]),
actualAmount: double.parse(this.form.value[incomeAmountLabel]),
frequency: this.form.value[frequencyLabel],
dateReceived: this.form.value[datePaidLabel].toString(),
category: this.form.value[categoryLabel],
depositAcct: this.form.value[depositToLabel],
description: this.form.value[descriptionLabel],
status: status,
weekNumber: isoCalendar.weekNumber,
monthNumber: Utilities.epochConverter("MONTH", this.form.value[datePaidLabel]),
calendarYear: isoCalendar.year,
isActive: isActiveY,
groupName: currentTransactions,
);
incomeList.add(income);
DateTime dateDerivedValue = this.form.value[datePaidLabel];
for (int i = 1; i <= Utilities.getFrequency(this.form.value[frequencyLabel]); i++) {
dateDerivedValue = Utilities.getDate(
this.form.value[frequencyLabel], dateDerivedValue, incomeTransaction, i
);
incomeList.add(new Income(
groupID: groupID,
expectedAmount: double.parse(this.form.value[incomeAmountLabel]),
actualAmount: double.parse(this.form.value[incomeAmountLabel]),
frequency: this.form.value[frequencyLabel],
dateReceived: dateDerivedValue.toString(),
category: this.form.value[categoryLabel],
depositAcct: this.form.value[depositToLabel],
description: this.form.value[descriptionLabel],
status: status,
weekNumber: isoCalendar.weekNumber,
monthNumber:
Utilities.epochConverter(
"MONTH", this.form.value[datePaidLabel]),
calendarYear: isoCalendar.year,
isActive: isActiveY,
groupName: currentTransactions,
)
);
}
objectBox.incomeBox.putMany(incomeList);
}
如您所见,我正在从incomeModel.dart 类中调用objectBox.incomeBox.putMany(incomeList)。objectBox 对象在主类中,所以我将它导入到 incomeModel 中,以便我可以访问它。但是,我收到以下错误
Bad state: failed to create cursor: 10001 Can not modify object of sync-enabled type "Income" because sync has not been activated for this store.
我不确定这意味着什么或该怎么做。我将有许多将插入数据的类,我需要从任何类访问存储,以便我可以插入、更新、删除数据。
有人可以帮我解决这个问题吗?我怎样才能使这项工作?提前致谢