0

I have a problem with my code, I wrote simple flutter app which is f note app, and I have included SQLite as a database , I run the app at first via the emulator and everything went cool , but when I tried to run it on my real device (which is an android device), the database did not respond (i.e I could not add new notes to the database ) and when I went back to run my app via the emulator .. the app did the same thing I found in my real device and in console I found this error

Error: Can't use 'SqfliteDatabaseException' because it is declared more than once.

I need help Please

shadowsheep
  • 14,048
  • 3
  • 67
  • 77
Karrar
  • 1,273
  • 3
  • 14
  • 30
  • Could you post also a minimal working example of your code (the one that gives you this error), so that we can help you based on the code you wrote? – shadowsheep Jan 20 '19 at 22:33
  • @shadowsheep here are the project files on GitHub https://github.com/KarrarMohamed/Notes-keeper-application – Karrar Jan 20 '19 at 22:53

1 Answers1

0

I saw your code and the problem is that the exception you get is probably related to this one:

PlatformException(sqlite_error, UNIQUE constraint failed: Notetable.id

And it's because you need to manage the unicity of your primary key when you insert a new row. You can have a look at this SO question for a quick reference.

So just to quickly make your code working I've made this changes (please take this code only for reference, write a better one):

  void createDataBase(Database db,int newVersion) async{
    await db.execute('CREATE TABLE IF NOT EXISTS $noteTable ($col_id INTEGER PRIMARY KEY ,'+
        '$col_title TEXT , $col_description TEXT , $col_date TEXT,$col_priority INTEGER)');
  }

And

Future<int> insertData(myNote note)async{
    var mdatabase = await database;
    var _newNoteMap = note.convertToMap();
    _newNoteMap['id'] = null;
    var result = await mdatabase.insert(noteTable, _newNoteMap);
    return result;
}

Pay attention that you always call a DB insert even when you update an existing note.

UPDATE: added additional modification (not listed before)

in databaseObject.dart

Map<String,dynamic> convertToMap(){
    var mapObject = Map<String,dynamic>();

    mapObject["id"]          =    _id;
    mapObject["title"]       =    _title;
    mapObject["description"] =    _description;
    mapObject["date"]        =    _date;
    mapObject["priority"]    =    _priority;

    return mapObject;
  }

in Note.dart

if(res >= 1){
        showAlertDialog('Status', "New note added successfully and the value of result is $res");

  }
shadowsheep
  • 14,048
  • 3
  • 67
  • 77
  • @Karrar I've posted all modification to your code to make it work. I know that works 'cause I've personally tested it. – shadowsheep Jan 25 '19 at 17:05