Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type Conversion issue Flutter – Convert from Future<List<Dog>?> to List<Dog>

Below is the method to retrieve a list of data from local sqlite table:

Future<List<Dog>?> retriveDogs() async {
    return await _dbHelper?.dogs();
  }

and you can check dogs() method as below :

Future<List<Dog>> dogs() async {
final db = await database;
final List<Map<String, dynamic>> maps = await db.query('dogs');
return List.generate(maps.length, (i) {
  return Dog(
    id: maps[i]['id'],
    name: maps[i]['name'],
    age: maps[i]['age'],
  );
});

}

I want to display list in my log or via print statement.
So, I have done as below :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(" >>> "+retriveDogs().toString());

But it gives me as :
>> Instance of ‘Future<List?> after print..

How can I get the complete list of Dogs ? Thanks.

>Solution :

Your retriveDogs can retune null value. You can pass empty list for null cases like.

Future<List<Dog>> retriveDogs() async {
    return await _dbHelper?.dogs()??[];
  }

and to get data from future, you can use await or .then

onPressed: () async {
  final data = await retriveDogs();
  print(data.toString());
},
retriveDogs().then((value) => print(value));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading