objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
1.05k stars 120 forks source link

Updated Object Not Persisting After PutAndGetAsync/PutAsync/Put in ObjectBox #667

Closed yashanghan22 closed 1 month ago

yashanghan22 commented 2 months ago

Why is it that when we update a list object and store it in ObjectBox using PutAndGetAsync, the logged value shows the updated ObjectBox, but in the next line, when we call the get method to retrieve the same object and print its value in the console, it still shows the old value?

Build info

Expected behavior

when i put updated ToMany List in objectbox it is not updating in local storage insertion and deletion is working fine but modificatin is not working.

Actual behavior

it should be update in local storage.

Code

Code ```dart final object = _getBillingStateById(); var inx = object.challanList.indexWhere((element) => element.id == challan.id); if (inx < 0) { return right(Failure()); } final ch = ChallanObjectModel.fromModel(challan); ch.partyName.target = PartyObjectModel.fromModel(challan.partyName); ch.qaulity.target = QaulityObjectModel.fromModel(challan.qaulity ?? QaulityModel()); object.challanList[inx] = ch; logger.f('aaa: ${object.challanList[inx].remarks}'); var i = await objectbox.billingStateBox.putAndGetAsync(object); logger.i('bbb;${i.challanList[inx].remarks}'); logger.w( 'ccc: ${(await objectbox.billingStateBox.getAsync(object.id))?.challanList[inx].remarks}'); return left(ch.id); ```

Logs, stack traces

TODO Add relevant logs, a stack trace or crash report.

Logs ```console - flutter: │ aaa: 123 - flutter: │ bbb;123 - flutter: │ ccc: 43 ```
greenrobot-team commented 2 months ago

Thanks for reporting! Is it possible that there are multiple objects inside challanList and the index inx for 123 is different?

Note: I labeled this issue with "more info required" so it will auto-close in a few days if there are no follow-up comments.

yashanghan22 commented 2 months ago

@greenrobot-team , inxis correct and i already checked.

greenrobot-team commented 2 months ago

Thanks! Then, there must be an issue somewhere else. Please provide enough additional information so I can reproduce this!

yashanghan22 commented 2 months ago

@greenrobot-team Here is some info regarding models

``This is my main Object:

@Entity()
class BillingStateObjectboxModel {
BillingStateObjectboxModel({
    required this.id,
    this.challanList = const [],
  });
@Id()
  Int id;
ToMany<ChallanObjectModel> challanList = ToMany();
}

challan object Model

class ChallanObjectModel {
  @Id()
  int id;
  double? amount;

  String? broker;

  double? cgst;

  String? challanNo;

@Property(type: PropertyType.date)
  DateTime? date;

  double? igst;

 @Property(type: PropertyType.floatVector)
  List<double>? meters;

  String? onAc;

  String? outside;

  String? outstate;

  PartyObjectModel? partyName;

  QaulityObjectModel? qaulity;

  double? rate;

  String? remarks;

  double? sgst;

  double? totalEnt;

  String? voucherNo;

ChallanObjectModel({
    this.id = 0,
    this.amount,
    this.broker,
    this.cgst,
    this.challanNo,
    this.date,
    this.igst,
    this.meters,
    this.onAc,
    this.outside,
    this.outstate,
    this.rate,
    this.remarks,
    this.sgst,
    this.totalEnt,
    this.voucherNo,
  });
}

The ChallanObjectModel is part of the BillingStateObjectboxModel. When I try to update any value in the challan object, it doesn't update. The update function is already shown in the first comment. I think this information should be enough, but let me know if you need more details.

greenrobot-team commented 2 months ago

Thanks for the details. I tried to verify this and:

the relation is properly updated.

  test('gh-667-put-and-get-async', () async {
    final box = store.box<BillingStateObjectboxModel>();
    final object = BillingStateObjectboxModel(id: 0);
    object.challanList.add(ChallanObjectModel(remarks: 'first'));
    box.put(object);

    final ch = ChallanObjectModel(remarks: 'second');
    object.challanList[0] = ch;
    var objectPut = await box.putAndGetAsync(object);
    print(objectPut.challanList[0].remarks);

    final objectGet = await box.getAsync(object.id);
    print(objectGet!.challanList[0].remarks);
  });

So this must be an issue with your implementation. In this case I can only help with a working test case that reproduces this.

Side note, this is not valid Dart syntax for me:

this.challanList = const []

Can you double-check this is similar to what the class actually looks like?

yashanghan22 commented 2 months ago

this.challanList = const []

What's the problem with this? Can you suggest a solution, please?

greenrobot-team commented 2 months ago

For me Dart says this is invalid syntax and can't compile.

Anyhow, that's not the main issue:

I can only help with a working test case that reproduces this.

github-actions[bot] commented 1 month ago

Without additional information, we are unfortunately not sure how to resolve this issue. Therefore this issue has been automatically closed. Feel free to comment with additional details and we can re-open this issue.