Abdullah Hamadi
Abdullah Hamadi

Reputation: 191

The return type 'Null' isn't a 'Widget', as required by the closure's context

I am having a few issues after migrating to flutter null-safety:

The return type 'Null' isn't a 'Widget', as required by the closure's context

The argument type 'Null' can't be assigned to the parameter type 'AuthenticationRepository'

The argument type 'Null' can't be assigned to the parameter type 'DatabaseRepository'

The argument type 'Null' can't be assigned to the parameter type 'RemoteConfig'

class _PlacesState extends State<_Places> {
  bool _isLoading = false;
  final TextEditingController _typeAheadController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return TypeAheadFormField<Map<String, dynamic>>(
      key: widget.formFieldKey,
      textFieldConfiguration: TextFieldConfiguration(
        autofocus: true,
        decoration: InputDecoration(
          hintText: 'Place',
          contentPadding: EdgeInsets.only(left: 8, right: 8),
          border: InputBorder.none,
        ),
        controller: _typeAheadController,
      ),
      suggestionsCallback: (String? pattern) async {
        final s = pattern?.trim();
        if (s == null || s == '' || s.length < 4) return null;
        _isLoading = true;
        try {
          await Future.delayed(Duration(seconds: 1));
          final data = await (_request(s) as FutureOr<Map<String, dynamic>>);
          final result = data['hits'] as List;
          return result.cast<Map<String, dynamic>>();
        } finally {
          _isLoading = false;
        }
      } as FutureOr<Iterable<Map<String, dynamic>>> Function(String),
      loadingBuilder: (BuildContext context) {
        if (!_isLoading) return null; **<-- Error is here**
        return Align(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 8),
            child: ExtendedProgressIndicator(),
          ),
        );
      },

--

    void main() {
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(App(
      authenticationRepository: null, <-- The argument type 'Null' can't be assigned to the parameter type 'AuthenticationRepository'
      databaseRepository: null, <-- The argument type 'Null' can't be assigned to the parameter type 'DatabaseRepository'
      remoteConfig: null, <-- The argument type 'Null' can't be assigned to the parameter type 'RemoteConfig'
    ));

    // Verify that our counter starts at 0.
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);

    // Tap the '+' icon and trigger a frame.
    await tester.tap(find.byIcon(Icons.add));
    await tester.pump();

    // Verify that our counter has incremented.
    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
  });
}

Upvotes: 1

Views: 6383

Answers (1)

lsaudon
lsaudon

Reputation: 1458

In your App widget, put nullable repositories.

final AuthenticationRepository? authenticationRepository

or Mock your repositories and give it to App instead of giving null.

Upvotes: 1

Related Questions