0

I'm trying to create a DropDownButton using the data from a table. I've created a function to return a list of objects which then can be used to convert those to a list of string but I keep getting "Null check operator used on a null value" error. So I tried to remove as many exclamation mark as I can. But I'm still getting the same error.

This is the function I use to get the list of objects from the DatabaseHelper class.

Future<List<Category>> getCategories() async {
  Database? db = await instance.database;
  var categories = await db?.query('Category', orderBy: 'id');

  List<Category> categoryList = categories!.isNotEmpty ? categories.map((c) => 
    Category.fromMap(c)).toList(): [];

  return categoryList;
}

This is the FutureBuilder and the DropDropButton

child: FutureBuilder<List<Category>> (
            future: DatabaseHelper.instance.getCategories(),
            builder: (BuildContext context, AsyncSnapshot<List<Category>> snapshot) {
              if (!snapshot.hasData) {
                return const Text('No data');
              }
              else {
                return DropdownButton(
                  onChanged: (value) {  },
                  items: snapshot.data?.map((category) =>
                      DropdownMenuItem<String>(
                        value: category.categoryName,
                        child: Text(category.categoryName),
                      )
                  ).toList(),
                );
              }
            },
          )

3 Answers3

1

Please change categories!.isNotEmpty to categories?.isNotEmpty

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • It says "A nullable expression can't be used as a condition." But I did try and change it to if (categories != null) but the same error still occurs. – BongBong123 Jun 17 '22 at 09:04
  • Which line throws this error? – Kaushik Chandru Jun 17 '22 at 10:14
  • The same line when I change the exclamation mark to question mark. But I don't think the problem is in the getCategories()? I tried changing the DropDownButton to ListView and it works. – BongBong123 Jun 17 '22 at 10:24
0

For some reason categories is null, i.e it's not properly fetched from the database. So this is leading to invoke isNotEmpty on a null value i.e null.isNotEmpty which is wrong, so the above error.

To overcome this , dont use the isNotEmpty property

Use something like :

 List<Category> categoryList = categories ? categories.map((c) => 
    Category.fromMap(c)).toList(): [];
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • I tried the code but it says "Conditions must have a static type of 'bool'." so I add categories != null in it. But I'm still getting the same error. I also tried to print out the query just to be sure and it has data in it. – BongBong123 Jun 17 '22 at 10:08
0

Setting the isExpanded to true solves my problem.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – lepsch Jun 18 '22 at 08:47