skip to Main Content

I took this function from https://suragch.medium.com/simple-sqflite-database-example-in-flutter-e56a5aaa3f91.

This works:

  import 'package:sqflite/sqflite.dart';

  Future<int> localDBQueryRowCount({required sqf.Database db, required String table}) async {
    final results = await db.rawQuery('SELECT COUNT(*) FROM $table');
    return Sqflite.firstIntValue(results) ?? 0;
  }

This doesn’t work:

  import 'package:sqflite/sqflite.dart' as sqf;

  Future<int> localDBQueryRowCount({required sqf.Database db, required String table}) async {
    final results = await db.rawQuery('SELECT COUNT(*) FROM $table');
    return sqf.firstIntValue(results) ?? 0; //ERROR HERE ON "firstIntValue": The function 'firstIntValue' isn't defined.
  }

Why does aliasing a package cause Android Studio to not be able to find the same function that it could find when the package was unaliased?

2

Answers


  1. Dart doesn’t have "real" aliases. as just add a prefix to the library.

    That’s why sqf.firstIntValue(results) doesn’t work. Instead, using sqf as a prefix, makes it work: sqf.Sqflite.firstIntValue(results).

    Reference: https://dart.dev/language/libraries#specifying-a-library-prefix

    Login or Signup to reply.
  2. If you use aliases import 'package:sqflite/sqflite.dart' as sqf; you should use this:

    return sqf.Sqflite.firstIntValue(results) ?? 0;
    

    Because you create alias sqf for file sqflite.dart and then you should to use the Sqflite class from this file. So you can do this by sqf.Sqflite.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search