Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InsertionAdapter not found in generated database file. #113

Closed
grappetite-ali opened this issue Apr 3, 2019 · 1 comment · Fixed by #114
Closed

InsertionAdapter not found in generated database file. #113

grappetite-ali opened this issue Apr 3, 2019 · 1 comment · Fixed by #114
Assignees
Labels
bug Something isn't working
Milestone

Comments

@grappetite-ali
Copy link

I created entity with name QuestionData and set its table name as questions.
When i ran command flutter packages pub run build_runner build, it generated database file with function _questionsInsertionAdapter being called in insert method, but it declared _questionDataInsertionAdapter in class _$QuestionsDao, instead of declaring _questionsInsertionAdapter. It caused syntax error of undefined _questionDataInsertionAdapter in generated file.

Below is are my files' code to better explain this bug.
questions-dao.dart

import 'package:floor/floor.dart';
import '../modals/response.dart';

@dao
abstract class QuestionsDao {
  @Query('SELECT * from ${QuestionData.TABLE_NAME}')
  Future<List<QuestionData>> findAllQuestions();

  @Query('SELECT * from ${QuestionData.TABLE_NAME} where isAnswered=false')
  Future<List<QuestionData>> findAllUnansweredQuestions();

  @Insert()
  Future<void> insertQuestions(List<QuestionData> questions);
}

response.dart

import 'package:floor/floor.dart';

@Entity(tableName: QuestionData.TABLE_NAME)
class QuestionData {
  static const TABLE_NAME = 'questions';

  @primaryKey
  int id;
  String questionOne;
  String questionTwo;
  int questionOneVoteCount;
  int questionTwoVoteCount;
  bool isAnswered = false;

  QuestionData();

  QuestionData.fromMap(Map<String, dynamic> map) {
    id = map['id'];
    questionOne = map['questionOne'];
    questionTwo = map['questionTwo'];
    questionOneVoteCount = map['questionOneVoteCount'];
    questionTwoVoteCount = map['questionTwoVoteCount'];
  }

  String getQuestionOneVotePercentage() {
    final percentage =
        (this.questionOneVoteCount + 1) / (this.getTotalVotesGiven() + 1) * 100;
    return '${percentage.toInt()}%';
  }

  String getQuestionTwoVotePercentage() {
    final percentage =
        (this.questionTwoVoteCount + 1) / (this.getTotalVotesGiven() + 1) * 100;
    return '${percentage.toInt()}%';
  }

  int getTotalVotesGiven() {
    return this.questionOneVoteCount + this.questionTwoVoteCount;
  }
}

akkh-database.dart

import 'dart:async';
import 'package:floor/floor.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart' as sqflite;
import '../modals/response.dart';
import 'questions-dao.dart';

part 'akkh-database.g.dart';

@Database(version: 1, entities: [QuestionData])
abstract class AkkhDatabase extends FloorDatabase {
  static Future<AkkhDatabase> openDatabase() async => _$open();

  QuestionsDao get questionsDao;
}

akkh-database.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'akkh-database.dart';

// **************************************************************************
// FloorGenerator
// **************************************************************************

Future<AkkhDatabase> _$open([List<Migration> migrations = const []]) async {
  final database = _$AkkhDatabase();
  database.database = await database.open(migrations);
  return database;
}

class _$AkkhDatabase extends AkkhDatabase {
  QuestionsDao _questionsDaoInstance;

  @override
  Future<sqflite.Database> open(List<Migration> migrations) async {
    final path = join(await sqflite.getDatabasesPath(), 'akkhdatabase.db');

    return sqflite.openDatabase(
      path,
      version: 1,
      onConfigure: (database) async {
        await database.execute('PRAGMA foreign_keys = ON');
      },
      onUpgrade: (database, startVersion, endVersion) async {
        MigrationAdapter.runMigrations(
            database, startVersion, endVersion, migrations);
      },
      onCreate: (database, version) async {
        await database.execute(
            'CREATE TABLE IF NOT EXISTS `questions` (`TABLE_NAME` TEXT, `id` INTEGER PRIMARY KEY NOT NULL, `questionOne` TEXT, `questionTwo` TEXT, `questionOneVoteCount` INTEGER, `questionTwoVoteCount` INTEGER, `isAnswered` INTEGER)');
      },
    );
  }

  @override
  QuestionsDao get questionsDao {
    return _questionsDaoInstance ??= _$QuestionsDao(database, changeListener);
  }
}

class _$QuestionsDao extends QuestionsDao {
  _$QuestionsDao(this.database, this.changeListener)
      : _queryAdapter = QueryAdapter(database),
        _questionDataInsertionAdapter = InsertionAdapter(
            database, 'questions', (QuestionData item) => <String, dynamic>{});

  final sqflite.DatabaseExecutor database;

  final StreamController<String> changeListener;

  final QueryAdapter _queryAdapter;

  final _questionsMapper = (Map<String, dynamic> row) => QuestionData();

  final InsertionAdapter<QuestionData> _questionDataInsertionAdapter;

  @override
  Future<List<QuestionData>> findAllQuestions() async {
    return _queryAdapter.queryList('SELECT * from questions', _questionsMapper);
  }

  @override
  Future<List<QuestionData>> findAllUnansweredQuestions() async {
    return _queryAdapter.queryList(
        'SELECT * from questions where isAnswered=false', _questionsMapper);
  }

  @override
  Future<void> insertQuestions(List<QuestionData> questions) async {
    await _questionsInsertionAdapter.insertList(
        questions, sqflite.ConflictAlgorithm.abort);
  }
}
@vitusortner vitusortner added the bug Something isn't working label Apr 3, 2019
@vitusortner
Copy link
Collaborator

Thanks for your report! I'm investigating the problem already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants