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 an example for StreamProvider #2004 #2021

Merged
merged 12 commits into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions website/docs/providers/stream_provider.mdx
Expand Up @@ -7,6 +7,8 @@ import TabItem from "@theme/TabItem";
import CodeBlock from "@theme/CodeBlock";
import configProvider from "!!raw-loader!/docs/providers/future_provider/config_provider.dart";
import configConsumer from "!!raw-loader!/docs/providers/future_provider/config_consumer.dart";
import streamProvider from "!!raw-loader!/docs/providers/stream_provider/live_stream_chat_provider.dart";
import streamConsumer from "!!raw-loader!/docs/providers/stream_provider/live_stream_chat_consumer.dart";
import { trimSnippet } from "../../src/components/CodeSnippet";

`StreamProvider` is similar to [FutureProvider] but for [Stream]s instead of
Expand Down Expand Up @@ -42,3 +44,14 @@ Using `StreamProvider` over [StreamBuilder] has numerous benefits:
[stream.periodic]: https://api.dart.dev/stable/2.15.1/dart-async/Stream/Stream.periodic.html
[family]: ../concepts/modifiers/family
[streambuilder]: https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html

## Usage example: listening live chats from server
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved

`StreamProvider` is used in when we handle stream of asynchronous data
such as Video Streaming, Weather broadcasting Apis.

<CodeBlock>{trimSnippet(streamProvider)}</CodeBlock>

Then, the UI can listen to live streaming chats like so:

<CodeBlock>{trimSnippet(streamConsumer)}</CodeBlock>
@@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'live_stream_chat_provider.dart';

/* SNIPPET START */
// UI part is same as like Future provider
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
Widget build(BuildContext context, WidgetRef ref) {
final liveChats = ref.watch(liveStreamProvider);
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
return liveChats.when(
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) => Text(error.toString()),
data: (msgs) {
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
return Text(msgs);
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
},
);
}
@@ -0,0 +1,26 @@
import 'dart:async';
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
import 'package:flutter_riverpod/flutter_riverpod.dart';
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved

/* SNIPPET START */
// Fake chats as data sorce
// Error last index is represent error which type is not a string
const fakeChats = ['John Hi', 'Flutter is Cute', 1];

// stream provider
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved
final liveStreamProvider = StreamProvider((ref) {
return liveStreamChat();
});
// fake chats come as Stream
Stream<String> liveStreamChat() async* {
for (var i = 0; i < fakeChats.length; i++) {
// Wait for a second
yield await Future.delayed(const Duration(seconds: 1));
try {
// yield chats
yield fakeChats[i] as String;
} catch (error) {
// handling error
yield* Stream.error(error);
}
}
}
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved