GetStream / stream-chat-flutter

Flutter Chat SDK - Build your own chat app experience using Dart, Flutter and the Stream Chat Messaging API.
https://getstream.io/chat/sdk/flutter/
Other
935 stars 343 forks source link

User became null after some time #464

Closed RizanPSTU closed 3 years ago

RizanPSTU commented 3 years ago

When I connect the user it gets connect but after some time. The user becomes null.

Future<void> main() async {
  //TODO Remove
  final String apiKey = "m3pkw92bhb2f";
  final StreamChatClient client = StreamChatClient(
    apiKey,
    logLevel: Level.ALL,
  );
  final userId = "nova1";
  final userTokenMain = client.devToken(userId);
  var connected = await client.connectUser(
    User(
      id: userId,
    ),
    userTokenMain,
  );
  doneToastUI(UIMessage("${connected?.me?.name}", true));

  //TODO Remove end
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  await ConfigReader.initialize();
  await Firebase.initializeApp();
  await configureInjection("dev");
  //TODO remove below
  FirebaseMessaging.instance.requestPermission();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
  //TODO end
  runApp(
    EasyLocalization(
      supportedLocales: [Locale('en', 'US'), Locale('es', 'US')],
      path: MoodAssets.moodTranslation,
      fallbackLocale: Locale('en', 'US'),
      child: MyApp(client),
    ),
  );
}

class MyApp extends StatelessWidget {
  final StreamChatClient client;

  MyApp(this.client);
  final theme = ThemeData(
    primarySwatch: Colors.green,
  );
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      builder: (context, child) {
        return StreamChat(
          child: child,
          client: client,
          streamChatThemeData: StreamChatThemeData.fromTheme(theme),
        );
      },
      routerDelegate: moodRouter.delegate(),
      routeInformationParser: moodRouter.defaultRouteParser(),
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      debugShowCheckedModeBanner: false,
      locale: context.locale,
    );
  }
}

What package are you using? What version? stream_chat_flutter: ^2.0.0-nullsafety.4

What platform is it about?

To Reproduce Steps to reproduce the behavior:

  1. use my api key
  2. connect user nova1
  3. See error

Expected behavior I user should not be null

Screenshots

Screenshot 2021-05-30 at 2 00 31 PM Screenshot 2021-05-30 at 1 49 29 PM Screenshot 2021-05-30 at 4 21 26 PM

ℹ️ 📡 handle new event: {type: health.check, cid: *, channel_id: null, channel_type: null, connection_id: 60a775a4-0a03-1a67-0000-000000851041, created_at: 2021-05-30T07:45:58.682437Z, me: null, user: null, message: null, channel: null, member: null, reaction: null, total_unread_count: null, unread_channels: null, online: null, parent_id: null, is_local: false}

✓] Flutter (Channel stable, 2.2.0, on macOS 11.3.1 20E241 darwin-x64, locale en-GB) • Flutter version 2.2.0 at /Users/rizan/flutter • Framework revision b22742018b (2 weeks ago), 2021-05-14 19:12:57 -0700 • Engine revision a9d88a4d18 • Dart version 2.13.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/rizan/Library/Android/sdk • Platform android-S, build-tools 30.0.3 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.5, Build version 12E262 • CocoaPods version 1.10.1

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)

[✓] VS Code (version 1.56.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.22.0

[✓] Connected device (2 available) • Android SDK built for x86 (mobile) • emulator-5554 • android-x86 • Android 10 (API 29) (emulator) • Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.77 ! Error: Rafsan Uddin Beg’s iPhone is not connected. Xcode will continue when Rafsan Uddin Beg’s iPhone is connected. (code -13)

deven98 commented 3 years ago

Hey @RizanPSTU,

The event you mention is a health check and does not imply that the user is not connected / null. If your list of channels does not fetch the appropriate channels, can you provide a snippet of your ChannelListView code?

Thanks.

RizanPSTU commented 3 years ago

This is the channel list page. @deven98

import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';

class TestChatList extends StatefulWidget {
  const TestChatList({Key? key}) : super(key: key);

  @override
  _TestChatListState createState() => _TestChatListState();
}

class _TestChatListState extends State<TestChatList> {
  @override
  void initState() {
    super.initState();
  }

  final Filter filter = Filter.and(
    [Filter.in_('members', [])],
  );
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ChannelsBloc(
        child: ChannelListView(
          filter: filter,
          sort: [SortOption('last_message_at')],
          pagination: PaginationParams(
            limit: 30,
          ),
          // channelPreviewBuilder: _channelPreviewBuilder,
          pullToRefresh: false,
          channelWidget: ChannelPage(),
        ),
      ),
    );
  }
}

class ChannelPage extends StatefulWidget {
  const ChannelPage({
    Key? key,
  }) : super(key: key);

  @override
  _ChannelPageState createState() => _ChannelPageState();
}

class _ChannelPageState extends State<ChannelPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: ChannelHeader(),
      body: Column(
        children: <Widget>[
          Expanded(
            child: MessageListView(),
          ),
          MessageInput(),
        ],
      ),
    );
  }
}
deven98 commented 3 years ago

@RizanPSTU Your filter seems to be off, leading the list not to load.

Can you let me know what you are trying to do?

For example, if you are trying to search for channels with no members, you can do:

final Filter filter = Filter.equal('members', []);

RizanPSTU commented 3 years ago

I want to view the channels that I am a member of. Even tho if I comment out the filter it's not loading anything. @deven98

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ChannelsBloc(
        child: ChannelListView(
          // filter: filter,
          sort: [SortOption('last_message_at')],
          pagination: PaginationParams(
            limit: 30,
          ),
          // channelPreviewBuilder: _channelPreviewBuilder,
          pullToRefresh: false,
          channelWidget: ChannelPage(),
        ),
      ),
    );
  }
}
RizanPSTU commented 3 years ago

I have found the solution. I did wrong. I should use filter this way, @deven98 Thank you very much.

    filter = Filter.and(
      [
        Filter.in_(
          'members',
          [StreamChat.of(context).user?.id ?? ""],
        ),
      ],
    );
deven98 commented 3 years ago

Happy to help :)

This should work too, btw:

filter = Filter.in_(
          'members',
          [StreamChat.of(context).user?.id ?? ""],
        );