Mauro Miotello
Mauro Miotello

Reputation: 21

Dart & SQFLite & DatabaseException

i'm new on Flutter/Dart, on my code i'm retrieving some data from a table and map it to a List My code works fine but i'd like to Handling errors & exceptions so i tried to use DatabaseException This is my code: my code I know that maybe the question is basic but i don't understand why list is no valid inside "if (e.isNoSuchTableError()) {"

Thanks for help

MM

Upvotes: 1

Views: 207

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

The list is defined inside a try block, which means its not in the scope of the catch block, that's why you cannot access it inside the catch block. You need to define it outside the try block to be able to access it:

List<Map> list = null;
try {
 list = await db.rawQuery(cmd);
} on DatabaseException catch(e){
   //access it
}

The same concept is used in methods:

void getData(){
  List<String> listOfNames =  List<String>();
}

void retrieveData(){
  print(listOfNames); // Undefined name 'listOfNames' - line 10
}

Upvotes: 1

Related Questions