why can't create user using stream chat api in flutter?

1.2k Views Asked by At

I'm trying to create a chat screen in my flutter application using Stream chat API. the problem is when I try to create a channel with two users it shows that the users are not created even though I have created them:

E/flutter ( 4695): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 
StreamChatNetworkError(code: 4, message: GetOrCreateChannel failed with error: "The following 
users are involved in channel create operation, but don't exist: [hasan11 mohammed11]. Please 
create the user objects before setting up the channel.", statusCode: 400, data: 
ErrorResponse(code: 4, message: GetOrCreateChannel failed with error: "The following users are 
involved in channel create operation, but don't exist: [hasan11 mohammed11]. Please create the 
user objects before setting up the channel.", statusCode: 400, moreInfo: 
https://getstream.io/chat/docs/api_errors_response))

here is my dart code for initializing the channel and the users:

onPressed () {
  await streamAPI.initUser(client,
    username: 'hasan',
    id: Config.hasanID,
    token: Config.hasanToken);
  final channel = await streamAPI.createChannel(
    client, 'messaging', 'sample', [Config.hasanID, Config.mohammedID]);
  Navigator.of(context).push(
    MaterialPageRoute(
      builder: (context) =>
        chat(client: client, channel: channel)));
}

StreamAPI.dart code:

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

class streamAPI {
  static Future initUser(
    StreamChatClient client, {
      @required String username,
      @required String id,
      @required String token
    }) async {
      final user = User(
        id: id,
        extraData: {
          'name': username
        }
      );
      await client.connectUser(user, token);
  }

  static Future<Channel> createChannel(
    StreamChatClient client,
    @required String type,
    @required String id,
    List<String> idMembers
  ) async {
    final channel = client.channel(type, id: id, extraData: {
      'name': 'channel1',
      'members': idMembers
    });
    await channel.create();
    channel.watch();
    return channel;
  }
}

Can anyone help me please?

1

There are 1 best solutions below

0
On BEST ANSWER

Make sure that these users are created on the Stream backend. In your Stream dashboard, go to the Explorer -> users section, and you should see a list of all your users. Ensure the IDs match what you have in your code.

Stream Dashboard Example

Take note you don't need to call both create and watch, as watch will automatically create the channel.

If you're using the Stream Chat UI or Core components, you don't even need to call watch yourself, then you only need to call create.

I recommend taking a look at the Flutter tutorial page if you're still stuck: https://getstream.io/chat/flutter/tutorial/

Or the Stream Flutter YouTube Playlist: https://www.youtube.com/watch?v=pO_MOJRqYlk&list=PLNBhvhkAJG6t-BxkRAnSqa67lm5C1mpKk