Update to Flutter/Dart database

Asked

Viewed 345 times

-1

I need to change only the comment field of a contact. But when I try to change this field accuses this error type 'int' is not a subtype of type 'Client'. I wonder why, since the update function passes the contact id.

Model function for comment update:

  updateComents(Client id) async {
    final db = await database;
    var res = await db.update("Contatos", id.toJson(),
        where: "id = ?", whereArgs: [id.id]);
    return res;
  }

Function of the select in the bank:

  Future<List> getRows() async{

    final db = await database;

    var res = await db.rawQuery("SELECT * FROM Contatos ORDER BY fullname");

    return res.toList();

  }

contactsList is assigned to getRows: contactsList = await getRows();

Button where I call this function:

child: CupertinoButton(
       onPressed: (){
           model.updateComents(model.contactsList[index]["id"]);
            },
            child: Text("Salvar Alterações")

       ),

I’m new to Dart/flutter with database.

  • What kind of list model.contactsList?

  • 1

    Is a variable assigned to a query in the database that sorts by name.

  • So what kind of list? For example, it is a list of this kind List<Client>? 'Cause if it’s you I could do it on the button model.updateComents(model.contactsList[index] without putting the ["id"]

  • Ah yes it is a Future<List>

  • Test the way I suggested in the comment above, if it works I create a more complete answer.

  • Not yet returns error type 'QueryRow' is not a subtype of type 'Client'

  • Not knowing the right type of list and what is received on it gets kind of hard... EDIT your question and put how you do to feed that list and what data it gets. And specify its type, for example Future<List<TipoTal>>

  • Debug your project as well and tell us if the error occurs at the button click or inside the method updateComents

  • 1

    I’m going to check what you asked for, I’m a little confused because it wasn’t me who created the code so until I understand I’m a little lost too.

  • The problem is that you’re passing one int to a function that awaits a Client. Probably the correct would be to call as suggested by @Matheusribeiro: model.contactsList[index].

  • Yes, I’ve just passed the model.contactsList[index] and returned the error type 'QueryRow' is not a subtype of type 'Client'

  • @Matheusribeiro made the edition, it was clearer this way ?

  • Yes, as soon as possible I create an answer.

  • @Matheusribeiro Thanks

Show 9 more comments

1 answer

0

In your class Client create the following method

void fromMap(Map<String, dynamic> json) {
    id: json["id"];
    fullname: json["fullname"];
}

Note: I used two fields as an example only, implement the others

Modify the click of your CupertinoButton for

child: CupertinoButton(
  onPressed: (){
    Client client = Client().fromMap(model.contactsList[index]);
    model.updateComents(client);
  },
  child: Text("Salvar Alterações")
)

If you don’t do it right

child: CupertinoButton(
  onPressed: (){
    Client client = Client();
    client.fromMap(model.contactsList[index]);
    model.updateComents(client);
  },
  child: Text("Salvar Alterações")
)

Note: I am currently without Flutter to take the full test

Explanation

The method rawQuery returns to us a Map<String, dynamic> then for you to call the updateComents you need to take the record in question and turn it into an object Client so you can pass as parameter of your method.

You can take a look at this link for a much more complete example Using Sqlite in Flutter

  • I understood, just one more doubt that I forgot to comment, how would take the value of the field that the user type that in my case is final controllerComent = TextEditingController(); ?

  • @Marcelolemos7 links the controller in your Textedit and then to get the data do var textoDigitado = controllerComent.Text. I advise you to read the Flutter documents to learn a few basic things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.