Getting Started with Agora RTC in Flutter: Complete Setup Guide
1. Introduction to Agora RTC
Real-Time Communication (RTC) enables mobile applications to stream low-latency audio and video between devices worldwide. In modern cross-platform development, Agora RTC provides a hardware-accelerated software development kit that handles complex Network Address Translation (NAT) traversal, jitter buffering, and media compression automatically.
In this guide, we will walk through setting up the official `agora_rtc_engine` package in a Flutter project for both Android and iOS platforms.
2. Prerequisites & Dependencies Setup
Before writing code, ensure you have an active Agora Developer Console account and have generated an App ID. Next, add `agora_rtc_engine` and `permission_handler` to your `pubspec.yaml` file:
dependencies:
flutter:
sdk: flutter
agora_rtc_engine: ^6.3.0
permission_handler: ^11.3.0
3. Android Platform Configuration
On Android, video and audio calling requires hardware access permissions for the device camera and microphone. Update `android/app/src/main/AndroidManifest.xml` with the following permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
4. iOS Platform Configuration
On iOS, add usage description keys to `ios/Runner/Info.plist` explaining why your app requires camera and microphone permissions:
<key>NSCameraUsageDescription</key>
<string>This app requires camera access for video consultation calls.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app requires microphone access for live voice calls.</string>
5. Initializing Agora RTC Engine
To initialize the engine in Flutter, request runtime permissions first using `permission_handler`, then instantiate `RtcEngine` using your App ID:
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:permission_handler/permission_handler.dart';
class AgoraService {
late final RtcEngine _engine;
final String appId = "YOUR_AGORA_APP_ID_HERE";
Future<void> initAgora() async {
// Request microphone & camera permissions
await [Permission.microphone, Permission.camera].request();
// Create RtcEngine instance
_engine = createAgoraRtcEngine();
await _engine.initialize(RtcEngineContext(
appId: appId,
channelProfile: ChannelProfileType.channelProfileCommunication,
));
// Enable video module
await _engine.enableVideo();
await _engine.startPreview();
// Register Event Handler
_engine.registerEventHandler(
RtcEngineEventHandler(
onJoinChannelSuccess: (RtcConnection connection, int elapsed) {
print("Successfully joined channel: ${connection.channelId}");
},
onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) {
print("Remote user joined: $remoteUid");
},
onUserOffline: (RtcConnection connection, int remoteUid, UserOfflineReasonType reason) {
print("Remote user left: $remoteUid");
},
),
);
}
}
6. Joining & Leaving Channels
To join a specific consultation room, invoke `joinChannel`. In production, token authentication generated from your backend server should be supplied for security:
Future<void> joinCall({required String channelName, required String tempToken, required int uid}) async {
await _engine.joinChannel(
token: tempToken,
channelId: channelName,
uid: uid,
options: const ChannelMediaOptions(
clientRoleType: ClientRoleType.clientRoleBroadcaster,
publishCameraTrack: true,
publishMicrophoneTrack: true,
),
);
}
Future<void> leaveCall() async {
await _engine.stopPreview();
await _engine.leaveChannel();
await _engine.release();
}
7. Production Best Practices
- Never hardcode tokens: Always fetch temporary RTC tokens dynamically from your secure backend server.
- Dispose resources: Unregister event handlers and call `release()` on page pop to prevent memory leaks and background camera activity.
- Handle connection drops: Listen to `onConnectionStateChanged` events to show a reconnection banner to users.
8. Conclusion
Agora RTC provides a foundation for building real-time mobile applications. In the next guide, we will explore building a complete 1-on-1 video call UI with local and remote video rendering.