user11934987
user11934987

Reputation:

How do I initialise a List?

How do I initialise a list? Of course I'm not talking about: List myList = []; Allow me to show you the issue:

1st I create my List variable (mainList)

List mainList;

Then I set it to get me the SQLITE output from here:

mainList =  await db.query(DBAssist.mainTable, where: "ItemCategory = ?", whereArgs: [categoryItem]);

And all is just dandy - except that the await is causing a lot of stress - and I need to initialise the List to something so I don't get the well known NULL error issue.

I'm just curious, but why doesn't

List mainList = []; 

Work?

Upvotes: 0

Views: 70

Answers (1)

Karim Elghamry
Karim Elghamry

Reputation: 1421

you can do the following:

mainList =  await db.query(DBAssist.mainTable, where: "ItemCategory = ?", whereArgs: [categoryItem]) ?? [];

this checks if the return of the the query is null and assigns an empty list instead.

Upvotes: 1

Related Questions