Search code examples
fluttergetstream-io

flutter chat app with stream.io Adding extraData into user properties


i am looking to add extraData into the user properties but i am having this error: The argument type 'List<Map<String, String>>' can't be assigned to the parameter type 'User'. i am currently using stream.io sdk for my flutter chat application. any help will be greatly appreciated.

Future<void> joinChannel(BuildContext context, int index) async {
    final core = StreamChatCore.of(context);
    final client = StreamChatCore.of(context).client;
    final channel = core.client.channel('messaging',
        id: _areaOfInterest[index].replaceAll(' ', ''),
        extraData: {
          'name': _areaOfInterest[index],
        });

    await channel.watch();

    await channel.addMembers([core.currentUser!.id]);

    await client.updateUser([
      {_areaOfInterest[index]: 'member'}
    ]);

    Navigator.of(context).push(
      CommunityChatScreen.routeWithChannel(channel),
    );
  }

Solution

  • The updateUser method expects a User object. You're trying to pass in a List.

    Here is an example of an approach you can take:

    final client = StreamChatCore.of(context).client;
    
    // Get the current user or create a user object and give needed info, `User();`
    final currentUser = client.state.currentUser!;
    
    // Get the extra data for the user and update with new info
    final extraData = currentUser.extraData;
    extraData['storeSomethingElse'] = 'something else';
    
    // Update user
    final updatedUser = currentUser.copyWith(
      extraData: extraData,
    );
    await client.updateUser(updatedUser);