1

I'm new to Flutter, how do I update certain objects/columns (not all columns)?

the table/entity class:

    @Entity()
    class Customer {
    int id;
    final String name;
    final String city;

    Customer ({this.id = 0, name= "", this.city = ""});
    }

let say...i want update
id=1,name="john",city="New York"
to
id=1,name="john",city="Tokyo"

the code:

    String strInput = ["1", "Tokyo"];
    List Lstr = (strInput).split(",");
    int idUpdate = int.tryParse(Lstr [0]) ?? 0;
    if (idUpdate > 0) {

                          customerBox.put(Customer(
                              id: idUpdate, city: Lstr [1]));
                      
                        }

the result = column 'name' filled with initial values :
id=1,name="",city="Tokyo"
thank you

0

1 Answer 1

1

ObjectBox works with Dart classes. I adapted the example from the README:

final customerNew = Customer(name: 'john', city: 'New York');
final id = box.put(customerNew);     // Create

final customerUpdate = box.get(id)!; // Read

customerUpdate.city = 'Tokyo';
box.put(customerUpdate);             // Update

If the assigned id is known, it's also possible to skip the read step and just put a Customer with that id:

final customerN = Customer(name: 'john', city: 'New York');
final id = box.put(customerN); // Create

final customerT = Customer(id: id, name: 'john', city: 'Tokyo');
box.put(customerT);            // Update
Sign up to request clarification or add additional context in comments.

1 Comment

I use the first method. it worked!you saved me a lot of time. Thank you very much final customerUpdate = box.get(id)!; // Read customerUpdate.city = 'Tokyo'; box.put(customerUpdate); // Update

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.