migrate example

This commit is contained in:
Salvatore Giordano
2021-04-19 15:23:00 +02:00
parent 5c5c4bbc3f
commit 098935834f
2 changed files with 21 additions and 18 deletions
+20 -17
View File
@@ -44,9 +44,9 @@ class StreamExample extends StatelessWidget {
/// To initialize this example, an instance of /// To initialize this example, an instance of
/// [client] and [channel] is required. /// [client] and [channel] is required.
const StreamExample({ const StreamExample({
Key key, Key? key,
@required this.client, required this.client,
@required this.channel, required this.channel,
}) : super(key: key); }) : super(key: key);
/// Instance of [StreamChatClient] we created earlier. /// Instance of [StreamChatClient] we created earlier.
@@ -69,28 +69,31 @@ class StreamExample extends StatelessWidget {
/// containing the channel name and a [MessageView] displaying recent messages. /// containing the channel name and a [MessageView] displaying recent messages.
class HomeScreen extends StatelessWidget { class HomeScreen extends StatelessWidget {
/// [HomeScreen] is constructed using the [Channel] we defined earlier. /// [HomeScreen] is constructed using the [Channel] we defined earlier.
const HomeScreen({Key key, @required this.channel}) : super(key: key); const HomeScreen({
Key? key,
required this.channel,
}) : super(key: key);
/// Channel object containing the [Channel.id] we'd like to observe. /// Channel object containing the [Channel.id] we'd like to observe.
final Channel channel; final Channel channel;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final messages = channel.state.channelStateStream; final messages = channel.state!.channelStateStream;
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text('Channel: ${channel.id}'), title: Text('Channel: ${channel.id}'),
), ),
body: SafeArea( body: SafeArea(
child: StreamBuilder<ChannelState>( child: StreamBuilder<ChannelState?>(
stream: messages, stream: messages,
builder: ( builder: (
BuildContext context, BuildContext context,
AsyncSnapshot<ChannelState> snapshot, AsyncSnapshot<ChannelState?> snapshot,
) { ) {
if (snapshot.hasData && snapshot.data != null) { if (snapshot.hasData && snapshot.data != null) {
return MessageView( return MessageView(
messages: snapshot.data.messages.reversed.toList(), messages: snapshot.data!.messages.reversed.toList(),
channel: channel, channel: channel,
); );
} else if (snapshot.hasError) { } else if (snapshot.hasError) {
@@ -119,9 +122,9 @@ class HomeScreen extends StatelessWidget {
class MessageView extends StatefulWidget { class MessageView extends StatefulWidget {
/// Message takes the latest list of messages and the current channel. /// Message takes the latest list of messages and the current channel.
const MessageView({ const MessageView({
Key key, Key? key,
@required this.messages, required this.messages,
@required this.channel, required this.channel,
}) : super(key: key); }) : super(key: key);
/// List of messages sent in the given channel. /// List of messages sent in the given channel.
@@ -135,8 +138,8 @@ class MessageView extends StatefulWidget {
} }
class _MessageViewState extends State<MessageView> { class _MessageViewState extends State<MessageView> {
TextEditingController _controller; late final TextEditingController _controller;
ScrollController _scrollController; late final ScrollController _scrollController;
List<Message> get _messages => widget.messages; List<Message> get _messages => widget.messages;
@@ -174,12 +177,12 @@ class _MessageViewState extends State<MessageView> {
reverse: true, reverse: true,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
final item = _messages[index]; final item = _messages[index];
if (item.user.id == widget.channel.client.uid) { if (item.user?.id == widget.channel.client.uid) {
return Align( return Align(
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
child: Padding( child: Padding(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
child: Text(item.text), child: Text(item.text ?? ''),
), ),
); );
} else { } else {
@@ -187,7 +190,7 @@ class _MessageViewState extends State<MessageView> {
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
child: Padding( child: Padding(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
child: Text(item.text), child: Text(item.text ?? ''),
), ),
); );
} }
@@ -246,5 +249,5 @@ class _MessageViewState extends State<MessageView> {
/// Helper extension for quickly retrieving /// Helper extension for quickly retrieving
/// the current user id from a [StreamChatClient]. /// the current user id from a [StreamChatClient].
extension on StreamChatClient { extension on StreamChatClient {
String get uid => state.user.id; String get uid => state.user!.id;
} }
+1 -1
View File
@@ -5,7 +5,7 @@ publish_to: "none"
version: 1.0.0+1 version: 1.0.0+1
environment: environment:
sdk: ">=2.7.0 <3.0.0" sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
cupertino_icons: ^1.0.0 cupertino_icons: ^1.0.0