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

type '_$Loading<int, Channel>' is not a subtype of type 'Success<int, Channel>' in type cast #1894

Open
1 task done
flodaniel opened this issue Apr 8, 2024 · 8 comments
Assignees
Labels
bug Something isn't working Stale

Comments

@flodaniel
Copy link
Contributor

Which packages are you using?

stream_chat, stream_chat_flutter, stream_chat_flutter_core, stream_chat_persistance, stream_chat_localizations

On what platforms did you experience the issue?

iOS, Android

What version are you using?

stream_chat_flutter: 8.0.0-beta.2
stream_chat_localizations: 8.0.0-beta.2
stream_chat_persistence: 8.0.0-beta.2

What happened?

We see this crash in our production apps, but don't know when and why it is occurring:

_TypeError: type '_$Loading<int, Channel>' is not a subtype of type 'Success<int, Channel>' in type cast
  File "paged_value_notifier.dart", line 123, in PagedValue.asSuccess
  File "paged_value_notifier.dart", line 32, in PagedValueNotifier.currentItems
  File "stream_channel_list_event_handler.dart", line 75, in StreamChannelListEventHandler.onChannelVisible

We use a StreamChannelListController to display a list of channels in our app.

Steps to reproduce

I can't reproduce this locally at the moment, but in all crashes we see we record a "foreground" or a "DEVICE_IDLE_MODE_CHANGED" (on android) event always right before this crash occurs.

Supporting info to reproduce

This is our my_channels_bloc, which us responsible for getting the list of channels to show

MyChannelsBloc Code
class MyChannelsBloc extends Bloc<MyChannelsEvent, MyChannelsState> {
  MyChannelsBloc(
    this._userRepository,
  ) : super(const MyChannelsInitial()) {
    _initController();

    on<MyChannelsLoaded>(_onMyChannelsLoaded);
    on<MyChannelsRefreshed>(_onMyChannelsRefreshed);
    on<MyChannelsListenerLoadSucceeded>(_onMyChannelsListenerLoadSucceeded);
    on<MyChannelsListenerLoadFailed>(_onMyChannelsListenerLoadFailed);
    on<MyChannelsListenerLoadStarted>(_onMyChannelsListenerLoadStarted);
  }

  final UserRepository _userRepository;

  late final StreamChannelListController _channelListController;

  late final void Function() _channelListener;

  @override
  Future<void> close() {
    _channelListController
      ..removeListener(_channelListener)
      ..dispose();
    return super.close();
  }

  void _onMyChannelsLoaded(
    MyChannelsLoaded event,
    Emitter<MyChannelsState> emit,
  ) async {
    await StreamChatService.existing().initialized;
    await _channelListController.loadMore(event.nextPageKey);
  }

  void _onMyChannelsRefreshed(
    MyChannelsRefreshed event,
    Emitter<MyChannelsState> emit,
  ) async {
    final user = await StreamChatService.existing().initialized;
    if (!user.online) {
      unawaited(_waitForReconnect());
      return;
    }
    await _channelListController.refresh(resetValue: true);
  }

  void _onMyChannelsListenerLoadSucceeded(
    MyChannelsListenerLoadSucceeded event,
    Emitter<MyChannelsState> emit,
  ) {
    emit(MyChannelsLoadSuccess(
      event.value.items,
      event.value.nextPageKey,
    ));
  }

  void _onMyChannelsListenerLoadFailed(
    MyChannelsListenerLoadFailed event,
    Emitter<MyChannelsState> emit,
  ) {
    emit(const MyChannelsLoadError());
  }

  void _onMyChannelsListenerLoadStarted(
    MyChannelsListenerLoadStarted event,
    Emitter<MyChannelsState> emit,
  ) {
    emit(const MyChannelsLoading());
  }

  Future<void> _initController() async {
    final user = await StreamChatService.existing().initialized;
    _channelListController = StreamChannelListController(
      client: StreamChatService.client,
      filter: Filter.and(
        [
          Filter.equal('type', 'messaging'),
          Filter.equal(
            'recommendationMode',
            RecommendationMode.groupsV2Recommendation
                .toSnakeCase()
                .toUpperCase(),
          ),
          Filter.in_(
            'members',
            [
              StreamChatHelper.convertUsernameToStreamUsername(
                  _userRepository.user.username)
            ],
          ),
        ],
      ),
    );
    _initChannelListener();
    if (!user.online) {
      add(const MyChannelsListenerLoadFailed());
      unawaited(_waitForReconnect());
      return;
    }

    await _channelListController.doInitialLoad();
  }

  Future<void> _waitForReconnect() async {
    await _channelListController.client.wsConnectionStatusStream
        .firstWhere((event) => event == ConnectionStatus.connected)
        .then((event) async {
      await _channelListController.doInitialLoad();
    });
  }

  void _initChannelListener() {
    _createChannelListener();
    _channelListController.addListener(_channelListener);
  }

  void _createChannelListener() {
    _channelListener = () {
      _channelListController.value.map(
        (paged_value_notifier.Success<int, Channel> value) {
          add(MyChannelsListenerLoadSucceeded(value));
        },
        loading: (_) {
          add(const MyChannelsListenerLoadStarted());
        },
        error: (e) {
          add(const MyChannelsListenerLoadFailed());
          unawaited(SentryService().recordError(
            e,
            stackTrace: StackTrace.current,
          ));
        },
      );
    };
  }
}

Relevant log output

No response

Flutter analyze output

No response

Flutter doctor output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@flodaniel flodaniel added the bug Something isn't working label Apr 8, 2024
Copy link

This issue is stale because it has been open for 20 days with no activity.

@github-actions github-actions bot added the Stale label Apr 28, 2024
@flodaniel
Copy link
Contributor Author

bump

@KestasVenslauskas
Copy link

Having the same issue here

@deven98 deven98 self-assigned this Jul 10, 2024
@deven98 deven98 removed the Stale label Jul 10, 2024
Copy link

This issue is stale because it has been open for 20 days with no activity.

@github-actions github-actions bot added the Stale label Jul 30, 2024
Copy link

github-actions bot commented Aug 7, 2024

This issue was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 7, 2024
@deven98 deven98 reopened this Sep 18, 2024
@deven98 deven98 removed the Stale label Sep 18, 2024
Copy link

github-actions bot commented Oct 8, 2024

This issue is stale because it has been open for 20 days with no activity.

@github-actions github-actions bot added the Stale label Oct 8, 2024
Copy link

This issue was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 15, 2024
@deven98 deven98 reopened this Oct 16, 2024
@deven98 deven98 removed the Stale label Oct 16, 2024
Copy link

github-actions bot commented Nov 5, 2024

This issue is stale because it has been open for 20 days with no activity.

@github-actions github-actions bot added the Stale label Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

3 participants