Skip to content

Mock provider extending $Notifier throws "Null check operator used on a null value" when accessing .notifier property #4371

@hibix43

Description

@hibix43

Describe the bug
When using a mock provider that extends $Notifier and implements a generated notifier class, accessing the .notifier property throws a "Null check operator used on a null value" error.

To Reproduce

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'mock_provider_test.g.dart';

void main() {
  test('Minimum sample code to describe the problem', () {
    final container = ProviderContainer.test(
      overrides: [sampleProvider.overrideWith(() => SampleNotifierMock())],
    );

    // Null check operator used on a null value
    container.read(sampleProvider.notifier);
  });
}

@riverpod
class SampleNotifier extends _$SampleNotifier {
  @override
  bool build() {
    return false;
  }
}

class SampleNotifierMock extends $Notifier<bool>
    with Mock
    implements SampleNotifier {}

Expected behavior
The .notifier property should return the mock instance without throwing a null check error, allowing proper testing of notifier-based providers with mocks.

Environment:

  • Flutter: 3.32.0
  • flutter_riverpod: ^3.0.3
  • riverpod_annotation: ^3.0.3
  • mockito: ^5.5.0

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions