0

I am new to objectbox in flutter and already getting an error while trying to put object in the store. I have the following code:

objectbox class

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);
  }
}

then on my main.dart file i have the following


/// 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
}

in another class called incomeModel.dart, i am trying to call the putMany function. here is partial code from the other class

 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);
  }

as you can see , i am calling objectBox.incomeBox.putMany(incomeList) from incomeModel.dart class. the objectBox object is in the main class so i am importing it in incomeModel so that i can access it. however, i am getting the following error

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.

i am not sure what this means or what to do. I will have many classes that will inserting data and i need to access the store from any class so that i can insert,update,delete data.

can someone helpme fix this? how can i make this work? thanks in advance

yoohoo
  • 1,147
  • 4
  • 22
  • 39

1 Answers1

0

I solved this problem by use objectbox_sync_flutter_libs instead of objectbox_flutter_libs in pubspec.yml

Syscall
  • 19,327
  • 10
  • 37
  • 52