Note: This blog has been updated since it was originally published in January 2020
A growing number of developers are discovering the benefits of building apps with the Flutter framework. Most appealing, the Flutter framework enables developers to build fully-functional applications for both Android & iOS platforms. One codebase for two applications means fewer man-hours and lower development costs. At the same time, Flutter’s rich choice of Material Design and Cupertino widgets, means applications have an native-like UI.
We’ve explored what is Flutter and why it’s a game changer in app development in an earlier blog as well as outlining what’s new in Futter. Today, we’re providing a step-by-step guide for how to build a messenger application using the QuickBlox Flutter SDK.
QuickBlox provides a comprehensive Flutter chat SDK available for download from our GitHub repository, that offers all you need to add real time communication to your application, including chat and video calling. It comes with rich documentation and code samples to help you get started.
Our chat SDK for Flutter contains a series of modules that enables you to add stunning functionality to create a modern chat messaging app:
To create a new Flutter chat messaging app with QuickBlox SDK from scratch follow these steps:
flutter create myapp
to create a new project.dependencies: quickblox_sdk: 0.0.1
You’re done with dependencies!
Now you can start the app with flutter run
But the application created is not really a chat messenger yet, right? So now you should create some UI for messages and then use our SDK to make it alive!
To make the SDK work, you should use your QuickBlox application account. To create a QuickBlox application, follow the steps below:
Init credentials:
const String APP_ID = "XXXXX"; const String AUTH_KEY = "XXXXXXXXXXXX"; const String AUTH_SECRET = "XXXXXXXXXXXX"; const String ACCOUNT_KEY = "XXXXXXXXXXXX"; const String API_ENDPOINT = ""; //optional const String CHAT_ENDPOINT = ""; //optional try { await QB.settings.init(APP_ID, AUTH_KEY, AUTH_SECRET, ACCOUNT_KEY, apiEndpoint: API_ENDPOINT, chatEndpoint: CHAT_ENDPOINT); } on PlatformException catch (e) { // Some error occured, look at the exception message for more details }
Now you should sign in:
try { QBLoginResult result = await QB.auth.login(userLogin, userPassword); QBUser qbUser = result.qbUser; QBSession qbSession = result.qbSession; } on PlatformException catch (e) { // Some error occured, look at the exception message for more details }
Once you’ve signed in you can connect to chat:
try { await QB.chat.connect(userId, userPassword); } on PlatformException catch (e) { // Some error occured, look at the exception message for more details }
Great! Now since you’re connected to chat – it’s time to send a message to someone.
Let’s create a new dialog:
try { QBDialog createdDialog = await QB.chat .createDialog(occupantsIds, dialogName, dialogType: dialogType); } on PlatformException catch (e) { // Some error occured, look at the exception message for more details }
In order to receive new messages we should tell the SDK to send us events when there are new messages in chat:
String eventName = QBChatEvents.RECEIVED_NEW_MESSAGE; try { await QB.chat.subscribeMessageEvents(dialogId, eventName, (data) { //receive a new message Map map = new Map.from(data); String messageType = map["type"]; if (messageType == QBChatEvents.RECEIVED_NEW_MESSAGE) { Map payload = new Map.from(map["payload"]); String messageBody = payload["body"]; String messageId = payload["id"]; } } on PlatformException catch (e) { // Some error occured, look at the exception message for more details }
So, now we have a dialog, we’ve subscribed for new messages and we’ve created a function to handle incoming messages. Let’s send our first message:
try { await QB.chat .sendMessage(dialogId, body: messageBody, saveToHistory: true); } on PlatformException catch (e) { // Some error occured, look at the exception message for more details }
That’s it!
The QuickBlox Flutter chat SDK expedites the process of messaging app development – from designing your initial prototype to final testing. Using our toolkit, you can integrate messaging, and voice & video calling into your app, manage user profiles, content, and data, and much more.
Check out our Flutter documentation & code samples.
For technical queries about using our SDK or recommendations for our product or documentation please submit a ticket to our support team.
Contact us to learn more about how QuickBlox can support your communication needs.