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

ChannelListView NULL error when a channel has no messages #6

Closed
funwithflutter opened this issue Mar 15, 2020 · 0 comments
Closed

ChannelListView NULL error when a channel has no messages #6

funwithflutter opened this issue Mar 15, 2020 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@funwithflutter
Copy link
Contributor

funwithflutter commented Mar 15, 2020

When creating a new channel (for example, from the CLI), that does not have any messages in the channel, will cause ChannelListView widget to throw an error as there are no checks to see if the channel message list is empty.

The method 'toLocal' was called on null.
Receiver: null
Tried calling: toLocal()

The relevant error-causing widget was
    ChannelListView 
lib/main.dart:147
When the exception was thrown, this was the stack
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
#1      ChannelPreview._buildDate.<anonymous closure> 
package:stream_chat_flutter/src/channel_preview.dart:71
#2      StreamBuilder.build 
package:flutter//widgets/async.dart:425
#3      _StreamBuilderBaseState.build 
package:flutter//widgets/async.dart:125
#4      StatefulElement.build 
package:flutter//widgets/framework.dart:4334
... 

There needs to some validation to make sure there are messages, before calling:

final lastMessageAt = snapshot.data.toLocal(); in "channel_preview.dart"

Widget _buildDate(BuildContext context) {
    return StreamBuilder<DateTime>(
      stream: channel.lastMessageAtStream,
      initialData: channel.lastMessageAt,
      builder: (context, snapshot) {
        final lastMessageAt = snapshot.data.toLocal();

        String stringDate;
        final now = DateTime.now();

        if (now.year != lastMessageAt.year ||
            now.month != lastMessageAt.month ||
            now.day != lastMessageAt.day) {
          stringDate = Jiffy(lastMessageAt.toLocal()).format('dd/MM/yyyy');
        } else {
          stringDate = Jiffy(lastMessageAt.toLocal()).format('HH:mm');
        }

        return Text(
          stringDate,
          style: StreamChatTheme.of(context).channelPreviewTheme.lastMessageAt,
        );
      },
    );
  }
@tbarbugli tbarbugli added the bug Something isn't working label Mar 15, 2020
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

No branches or pull requests

3 participants