Search code examples
flutterdartsqflite

how to transform a Future<dynamic> to string in dart:flutter


I’m using some sqflite to do a query on a database, and extract a specific name of a table, for example: I have 3 names, Roberto, Ricardo and Andres, and I want to obtain only the name of Roberto, so this is what im doing:

loginLogic() async {
  var X = 'Roberto';
  final db = await database;
  List<Map> result = await db.rawQuery('SELECT * FROM Usuario WHERE username=?', [X]);

So now, I want to compare this result with another one to do an "if" function, just like this:

if(result==X){
  return 1;
} else {
  return 0;
}

But this is what flutter says:

List<Map<dynamic, dynamic>> result
Equality operator '==' invocation with references of unrelated

And the Debug console:

type 'Future<dynamic>' is not a subtype of type 'int'

So I need a way to compare the value that results from the list of users with the string with the variable I declarated 'X'.

Try using the reference of this problem

This is what I’ve tried:

loginLogicpar() async {
  var X = 'Giova';
  final db = await database;
  final result = await db.rawQuery('SELECT * FROM Usuario WHERE username=?', [X]);
  return result;
}

loginLogic() async {
  var X = 'Giova';
  final user = await loginLogicpar();
  if(user==X){
    return 1;
  }
  return 0;
}

And returns the exact same result: type 'Future' is not a subtype of type 'int'

Added the code so you can replicate it, just be sure you have sqflite on your depenencies.


Solution

  • It was a hell of a road, but i made it with the help of you all, but the final piece was @mutantkeyboard, what i was getting was a list of the different elements from the table of Sqflite, but after converting it with a variable and the .first function i get to transform it to another variables! here's what i made:

    loginLogic() async {
      final db = await database;
      var table = await db.rawQuery("SELECT MAX(id)+1 as id FROM Usuario");
      int id = table.last["id"];
      for(var i=1; i<id; i++){
        var result = await db.rawQuery('SELECT username FROM Usuario WHERE id=?', [i]);
        String Y = result.first["username"];
        if(Y=='Roberto'){
          return 1;
        }
      }
      return 0;
    }
    

    if you want to compare it with another variable outside, for example, an auth for a local-login (that's what i was doing, just for educational purpose, i'll NEVER suggest you to put your usernames and passwords inside your local software) and compare the textfield.text with the table, then you can modify the code to something like this:

    loginLogic(String field) async {
      final db = await database;
      var table = await db.rawQuery("SELECT MAX(id)+1 as id FROM Usuario");
      int id = table.last["id"];
      for(var i=1; i<id; i++){
        var result = await db.rawQuery('SELECT username FROM Usuario WHERE id=?', [i]);
        String Y = result.first["username"];
        if(Y==field){
          return 1;
        }
      }
      return 0;
    }