Minimal reproducible code:
StreamController<int> _streamController = StreamController();
final provider1 = StreamProvider<int?>((ref) {
return _streamController.stream.map((value) {
print('listener1()');
return value;
});
});
final provider2 = StreamProvider<int?>((ref) {
return ref.watch(provider1).maybeWhen(
orElse: () => Stream.value(null),
data: (value) {
print('listener2()');
return Stream.value(value);
},
);
});
class FooPage extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
ref.watch(provider2);
return Scaffold(
body: ElevatedButton(
onPressed: () async {
await _streamController.addStream(Stream.value(42));
print('Done');
},
child: Text('Button'),
),
);
}
}
When you press the button, it prints:
I/flutter (15845): listener1()
I/flutter (15845): Done
I/flutter (15845): listener2()
But the correct order should be:
I/flutter (15845): listener1()
I/flutter (15845): listener2()
I/flutter (15845): Done
So, how can I fix this delay? Who is causing it?