riverpod icon indicating copy to clipboard operation
riverpod copied to clipboard

Watching selectAsync within a provider chain never completes

Open mahmuto opened this issue 1 year ago • 3 comments

This one is a little difficult to put into words. I have a scenario where a widget watches a provider that uses a chain of providers. Within on of these providers, a selectAsync is being watched but never produces a value. However, when reading the selectAsync or when reading/watching the future, the provider produces a value. This isn't desirable in all cases though.

Any help would be appreciated, thank you!

lib/providers.dart in the attached sample contains the gist of the issue:

import 'package:flutter/foundation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'providers.g.dart';

@riverpod
Future<int> value0(Value0Ref ref) async {
  debugPrint('value0');
  await Future.delayed(const Duration(milliseconds: 1500));
  return 0;
}

@riverpod
Future<int> value1(Value1Ref ref) async {
  debugPrint('value1');

  // ISSUE: When watching 'selectAsync', the future never completes.
  return await ref.watch(value0Provider.selectAsync((data) => data));

  // However, if we read instead of watch, the future does complete.
  // return await ref.read(value0Provider.selectAsync((data) => data));

  // Also, if we 'read' or 'watch' 'future' instead of using 'selectAsync', the future also completes.
  // return await ref.read(value0Provider.future);
  // return await ref.watch(value0Provider.future);
}

@riverpod
Future<int> value2(Value2Ref ref) async {
  debugPrint('value2');
  return await ref.read(value1Provider.future);
}

@riverpod
Future<int> value3(Value3Ref ref) async {
  debugPrint('value3');
  return await ref.read(value2Provider.future);
}

To Reproduce Run the following sample flutter project. riverpod_issue.zip

Expected behavior The expectation is the widget should update after loading is complete, but never does. See the comments in lib/provider.dart for what does make it work.

mahmuto avatar May 11 '23 20:05 mahmuto