import 'package:flutter/material.dart';
import 'package:revocare_chat_app/pages/profilePage.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:sendbird_sdk/sendbird_sdk.dart' hide ConnectionState;
// import 'package:sendbird_sdk/sendbird_sdk.dart';
// ignore: depend_on_referenced_packages
class SendbirdManager {
static final SendbirdSdk instance = SendbirdSdk(appId: dotenv.env['APP_ID']);
static Future<SendbirdSdk> connect(String userId) async {
try {
if (instance.currentUser != null &&
instance.currentUser?.userId != userId) {
await instance.disconnect();
}
final user = await instance.connect(userId);
print('Connected to SendBird as $userId');
} catch (e) {
print('Error connecting to SendBird: $e');
}
return instance;
}
}
class Home extends StatelessWidget {
final String? email;
const Home({Key? key, required this.email}) : super(key: key);
Future<List<GroupChannel>> fetchChats() async {
try {
if (email != null) {
await SendbirdManager.connect(email!);
} else {
print('Email is null');
return [];
}
final query = GroupChannelListQuery()
..includeEmptyChannel = true
..order = GroupChannelListOrder.latestLastMessage
..limit = 15;
final channels = await query.loadNext();
return channels;
} catch (e) {
print('Error fetching chats: ${e.toString()}');
if (e is BadRequestError) {
print('Detailed Error: ${e.message}');
}
return [];
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Home"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.account_circle),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProfilePage(userEmail: email)),
);
},
),
],
),
body: FutureBuilder<List<GroupChannel>>(
future: fetchChats(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text("Error: ${snapshot.error}"));
} else if (snapshot.hasData) {
List<GroupChannel> channels = snapshot.data!;
return ListView.builder(
itemCount: channels.length,
itemBuilder: (context, index) {
GroupChannel channel = channels[index];
return ListTile(
title: Text(channel.name ?? 'Default Name'),
subtitle: Text("Last message: ${channel.lastMessage}"),
onTap: () {
// Handle tap, possibly navigate to the chat screen
},
);
},
);
} else {
return Center(child: Text("No chats found"));
}
},
),
);
}
}
this is my code SendbirdManager.connect function works corectly.
code braks from final channels = await query.loadNext();
here
it showing
flutter: Error fetching chats: Instance of 'BadRequestError'
flutter: Detailed Error: Not authorized. "Session-Key doesn't match user_id".
this error message
how to hadle this