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

Manually set state breaks return value of FutureProvider and AsyncNotifierProvider #1997

Closed
trejdych opened this issue Dec 12, 2022 · 0 comments · Fixed by #2019
Closed

Manually set state breaks return value of FutureProvider and AsyncNotifierProvider #1997

trejdych opened this issue Dec 12, 2022 · 0 comments · Fixed by #2019
Labels
bug Something isn't working

Comments

@trejdych
Copy link
Sponsor Contributor

Describe the bug
When I manually set a state in a FutureProvider or AsyncNotifier build method the returned value is ignored. The problem exists for both syntaxes.
so for the someProvider (from the snippet below), I got the states:

  1. AsyncLoading()
  2. AsyncData(value:1)
  3. AsyncData(value:2)

To Reproduce

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

final someProvider = FutureProvider<int>((ref) async {
  await Future.delayed(const Duration(seconds: 1));
  ref.state = const AsyncData(1);
  await Future.delayed(const Duration(seconds: 1));
  ref.state = const AsyncData(2);
  await Future.delayed(const Duration(seconds: 1));
  return 3;
});

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

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

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              ref.watch(someProvider).toString(),
            ),
            TextButton(
              onPressed: () => ref.invalidate(someProvider),
              child: const Text('invalidate'),
            ),
          ],
        ),
      ),
    );
  }
}

environment:

Flutter 3.3.9
flutter_riverpod: 2.1.1

Expected behavior
The returned value should also be set as the provider's state.
So the result should be

  1. AsyncLoading()
  2. AsyncData(value:1)
  3. AsyncData(value:2)
  4. AsyncData(value:3)
@trejdych trejdych added bug Something isn't working needs triage labels Dec 12, 2022
rrousselGit added a commit that referenced this issue Dec 21, 2022
@rrousselGit rrousselGit linked a pull request Dec 21, 2022 that will close this issue
rrousselGit added a commit that referenced this issue Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants