Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
06f98c2
commit 8b6d545
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
website/docs/providers/stream_provider/live_stream_chat_consumer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
import 'live_stream_chat_provider.dart'; | ||
|
||
/* SNIPPET START */ | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
final liveChats = ref.watch(chatProvider); | ||
// Like FutureProvider, it is possible to handle loading/error states using AsyncValue.when | ||
return liveChats.when( | ||
loading: () => const CircularProgressIndicator(), | ||
error: (error, stackTrace) => Text(error.toString()), | ||
data: (messages) { | ||
// Display all the messages in a scrollable list view. | ||
return ListView.builder( | ||
// Show messages from bottom to top | ||
reverse: true, | ||
itemCount: messages.length, | ||
itemBuilder: (context, index) { | ||
final message = messages[index]; | ||
return Text(message); | ||
}, | ||
); | ||
}, | ||
); | ||
} |
18 changes: 18 additions & 0 deletions
18
website/docs/providers/stream_provider/live_stream_chat_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
/* SNIPPET START */ | ||
final chatProvider = StreamProvider<List<String>>((ref) async* { | ||
// Connect to an API using sockets, and decode the output | ||
final socket = await Socket.connect('my-api', 4242); | ||
ref.onDispose(socket.close); | ||
|
||
var allMessages = const <String>[]; | ||
await for (final message in socket.map(utf8.decode)) { | ||
// A new message has been received. Let's add it to the list of all messages. | ||
allMessages = [...allMessages, message]; | ||
yield allMessages; | ||
} | ||
}); |