floor icon indicating copy to clipboard operation
floor copied to clipboard

NoSuchMethodError: The getter 'name' was called on null. Receiver: null Tried calling: name

Open nawinkhatiwada opened this issue 4 years ago • 8 comments

Error while running flutter pub run build_runner build

Error in AndroidStudio Logcat

NoSuchMethodError: The getter 'name' was called on null.
Receiver: null
Tried calling: name

VERSION USED:

dependencies:
  floor: ^0.13.0
dev_dependencies:
  floor_generator: ^0.13.0
  build_runner: ^1.7.3

My DAO Class

import 'package:floor/floor.dart';

@dao
abstract class UsersDao {

  @Query('select id from users')
  Future<int> getUser();
}

ENTITY CLASS:

import 'package:floor/floor.dart';

@Entity(tableName: 'users')
class Users {
  @PrimaryKey(autoGenerate: true)
  @ColumnInfo(name: 'id', nullable: true)
  final int id;
  Users(this.id);
}

DATABASE CLASS

import 'package:retailer_flutter/storage/database/dao/user_dao.dart';
import 'package:retailer_flutter/storage/database/entity/user_entity.dart';
import 'package:sqflite/sqflite.dart' as sqflite;
import 'dart:async';
import 'package:floor/floor.dart';

part 'rosia_database.g.dart';

@Database(version: 1, entities: [Users])
abstract class RosiaDatabase extends FloorDatabase {
  static RosiaDatabase _database;

  static RosiaDatabase getInstance() {
    if (_database == null) {
      _database = $FloorRosiaDatabase
          .databaseBuilder("rosia_database.db")
          .build() as RosiaDatabase;
    }
    return _database;
  }

  UsersDao get userDao;
}

When ever i try to query id from users with return type of Future<int> the code generation does not work. But when the return type is Future<void> the code generation is successful where am I wrong?

@Query('select id from users')
 Future<void> getUser();   // works (the code generation is successful)

@Query('select id from users')
 Future<int> getUser();   // not working

@Query('select * from users')
 Future<Users> getUser();  // this also works

How to query single column like id, name, etc.? I don't see any example where i can query a single column name like id.

nawinkhatiwada avatar Jul 15 '20 06:07 nawinkhatiwada

Single-column return for arbitrary queries is not yet supported and will come with #361. See also #200.

mqus avatar Jul 15 '20 06:07 mqus

When will this feature be released? Is there any estimated date for the release? If it is going to take some time then I will rather shift to another library. There are numerous use-cases like this in my project.

nawinkhatiwada avatar Jul 15 '20 06:07 nawinkhatiwada

This feature is mostly ready functionally. There are some tests missing and it was not reviewed yet, so I think it will probably take at least another month until the release.

mqus avatar Jul 15 '20 19:07 mqus

But if you are willing to accept a few more bugs(that I don't yet know of) and 5 Minutes of effort, you can clone the PR branch into your project (e.g. as a git submodule) and add it to your pubspec.yaml If you find any bugs then I will try to fix them asap(think hours)

mqus avatar Jul 15 '20 19:07 mqus

I will give it a go and ping you if I have any issues.

nawinkhatiwada avatar Jul 16 '20 04:07 nawinkhatiwada

I had a similar issue, and ended up creating a DatabaseView, and running the query via that.

inboxg9 avatar Jul 18 '20 07:07 inboxg9

@mqus Has this feature been released?

nawinkhatiwada avatar Oct 06 '20 10:10 nawinkhatiwada

No. The review is still pending.

mqus avatar Oct 06 '20 13:10 mqus