Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoDisposeProvider is getting disposed when watched in a StreamProvider using async* #3526

Closed
ValentinVignal opened this issue May 6, 2024 · 1 comment
Assignees
Labels
bug Something isn't working needs triage

Comments

@ValentinVignal
Copy link
Contributor

Describe the bug

When watched in a StreamProvider using async*, an auto dispose provider is getting disposed even when watched.

To Reproduce

Run flutter run on the code sample.
Tap on the floating action button to increment the state of the stateNotifier.
Notice that the state is disposed even though it is watched.

Code sample

You can also checkout https://github.com/ValentinVignal/flutter_app_stable/tree/riverpod/autodipose-provider-getting-disposed-in-async

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

final stateProvider = StateProvider.autoDispose<int>((ref) => 0);

final streamProvider = StreamProvider.autoDispose<int>(
  (ref) async* {
    final state = ref.watch(stateProvider);
    for (var i = 0; i < 10; i++) {
      await Future.delayed(const Duration(seconds: 1));
      yield state * 100 + i;
    }
  },
  dependencies: [stateProvider],
);

// Note: it works properly if `streamProvider` is replaced with:

// Stream<int> _buildStream(int value) async* {
//   for (var i = 0; i < 10; i++) {
//     await Future.delayed(const Duration(seconds: 1));
//     yield value * 100 + i;
//   }
// }

// final streamProvider = StreamProvider.autoDispose<int>(
//   (ref) {
//     final state = ref.watch(stateProvider);
//     return _buildStream(state);
//   },
//   dependencies: [stateProvider],
// );

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends ConsumerWidget {
  const Home({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final value = ref.watch(streamProvider).valueOrNull ?? 0;
    return Scaffold(
      body: Center(
        child: Text('Value: $value'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          ref.read(stateProvider.notifier).state++;
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

Expected behavior

I would expect the async* behavior to be the same as the one without using async* (aka: the state not being disposed)

I'm using

  • Flutter 3.19.6
  • flutter_riverpod 2.5.1
  • riverpod 2.5.1
@rrousselGit
Copy link
Owner

This is technically expected.
The issue is async* here.

When using async*, anything inside the function is delayed by bit.

This is the different between:

Future<void> fn(ref) {
  ref.watch(a);
}

and:

Future<void> fn(ref) {
  await null;
  ref.watch(a);
}

Ultimately, this should be fixed alongside the FutureProvider issue when #1253 is implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants