Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

[BUG] isar_generator Crashes with "Null check operator used on a null value" for Composite Index #126

@tomkingroy

Description

@tomkingroy

[BUG] isar_generator Crashes with "Null check operator used on a null value" for Composite Index

Description

When using a composite index in an Isar collection with fields like int and DateTime, the isar_generator fails during code generation with the error:

[SEVERE] isar:isar_generator on lib/db/models/chat_message/chat_message.dart (cached):
Null check operator used on a null value

This bug prevents the generation of the *.g.dart file, blocking the use of the collection. The issue appears related to the composite index definition, as removing the composite index allows the generator to succeed. This behavior suggests a parsing bug in isar_generator when handling composite indexes, possibly involving late fields or specific field types.

Steps to Reproduce

  1. Create a Flutter project with Isar dependencies (isar: ^4.0.3, isar_flutter_libs: ^4.0.3).
  2. Define a collection with a composite index on an int field referencing a DateTime field.
  3. Run dart run build_runner build to generate the *.g.dart file.
  4. Observe the build failure with the error: Null check operator used on a null value.

Code Sample

import 'package:isar/isar.dart';

part 'chat_message.g.dart';

@collection
class ChatMessage {
  @Id()
  late int id;
  late String content;
  @Index(composite: [CompositeIndex('createdAt')])
  late int sessionId;
  late DateTime createdAt;
  late DateTime updatedAt;
}

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  isar:
    hosted: https://pub.isar-community.dev
    version: ^4.0.3
  isar_flutter_libs:
    hosted: https://pub.isar-community.dev
    version: ^4.0.3
  path_provider: ^2.1.5

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.4.15

Run

flutter clean
flutter pub get
dart run build_runner build

Expected Behavior

  • The isar_generator should successfully generate chat_message.g.dart without errors.
  • The composite index (sessionId, createdAt) should be created, enabling efficient queries like:
isar.chatMessages
    .filter()
    .sessionIdEqualTo(123)
    .sortByCreatedAtDesc()
    .findAll();

Actual Behavior

Build fails with:

[SEVERE] isar:isar_generator on lib/db/models/chat_message/chat_message.dart (cached):
Null check operator used on a null value
[SEVERE] Failed after 99ms

No chat_message.g.dart is generated, preventing database usage.

Workarounds

  • Remove composite index:

    @Index()
    late int sessionId;

    This allows generation but loses composite index benefits.

  • Use single indexes:

    @Index()
    late int sessionId;
    @Index()
    late DateTime createdAt;

    This works but is less efficient for sorting queries.

  • Remove late modifiers (not viable due to null safety errors without initialization)

Environment

  • Isar Version: 4.0.3 (https://pub.isar-community.dev)
  • Dart SDK: 3.7.2
  • Flutter Version: 3.29.3
  • Operating System: macOS 15.5 (24F74)
  • Build Runner: 2.4.15

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions