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

Add FutureSignal that wraps futures #7

Merged
merged 4 commits into from
Nov 24, 2023

Conversation

jinyus
Copy link
Contributor

@jinyus jinyus commented Nov 24, 2023

Re: #6

This class makes it straightforward to reset the state of the signal.

Usage:

fetchData() {
  return 'abc';
}

final futureSignal = FutureSignal<String>(
  () => Future.delayed(const Duration(seconds: 1), () => fetchData()),
);

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  void _reloadData() {
    futureSignal.reset();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Builder(builder: (context) {
              final state = futureSignal.watch(context);
              final text = switch (state) {
                SignalError(value: var v) => 'Error: $v',
                SignalValue(value: var v) => 'Success: $v',
                _ => 'Loading...',
              };
              return Text(
                text,
                style: Theme.of(context).textTheme.headlineMedium!,
              );
            }),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _reloadData,
        child: const Icon(Icons.refresh),
      ),
    );
  }
}

@rodydavis rodydavis self-requested a review November 24, 2023 20:58
Copy link
Owner

@rodydavis rodydavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rodydavis
Copy link
Owner

I am going to add back the timeout, and add the same for Stream. But LGTM!

@rodydavis rodydavis merged commit 8ae58bd into rodydavis:main Nov 24, 2023
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants