Skip to content

Commit

Permalink
feat: upgrade to dart 2.12.0 and migrating to null safety #264 #265
Browse files Browse the repository at this point in the history
  • Loading branch information
LichKing-2234 committed Mar 8, 2021
1 parent 7c95644 commit b555e37
Show file tree
Hide file tree
Showing 32 changed files with 938 additions and 934 deletions.
5 changes: 5 additions & 0 deletions example/lib/config/agora.config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const appId = YOUR_APP_ID;
/// Please refer to https://docs.agora.io/en/Agora%20Platform/token
const token = YOUR_TOEKN;

/// Your channel ID
const channelId = YOUR_CHANNEL_ID;

/// Your int user ID
const uid = YOUR_UID;

/// Your string user ID
const stringUid = YOUR_STRING_UID;
1 change: 1 addition & 0 deletions example/lib/examples/advanced/index.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:agora_rtc_engine_example/examples/advanced/multi_channel.dart';

/// Data source for advanced examples
final Advanced = [
{'name': 'Advanced'},
{'name': 'MultiChannel', 'widget': MultiChannel()}
Expand Down
104 changes: 50 additions & 54 deletions example/lib/examples/advanced/multi_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

const channelId0 = 'channel0';
const channelId1 = 'channel1';
const _channelId0 = 'channel0';
const _channelId1 = 'channel1';

/// MultiChannel Example
class MultiChannel extends StatefulWidget {
RtcEngine _engine = null;
RtcChannel _channel0 = null, _channel1 = null;

@override
State<StatefulWidget> createState() => _State();
}

class _State extends State<MultiChannel> {
String renderChannelId;
late final RtcEngine _engine;
late final RtcChannel _channel0, _channel1;
String? renderChannelId;
bool isJoined0 = false, isJoined1 = false;
List<int> remoteUid0 = [], remoteUid1 = [];

Expand All @@ -36,55 +35,52 @@ class _State extends State<MultiChannel> {
@override
void dispose() {
super.dispose();
widget._engine?.destroy();
_engine.destroy();
}

_initEngine() async {
widget._engine =
await RtcEngine.createWithConfig(RtcEngineConfig(config.appId));
_engine = await RtcEngine.createWithConfig(RtcEngineConfig(config.appId));

await widget._engine.enableVideo();
await widget._engine.startPreview();
await widget._engine.setChannelProfile(ChannelProfile.LiveBroadcasting);
await widget._engine.setClientRole(ClientRole.Broadcaster);
await _engine.enableVideo();
await _engine.startPreview();
await _engine.setChannelProfile(ChannelProfile.LiveBroadcasting);
await _engine.setClientRole(ClientRole.Broadcaster);
}

_joinChannel0() async {
if (defaultTargetPlatform == TargetPlatform.android) {
await [Permission.microphone, Permission.camera].request();
}

widget._channel0 = await RtcChannel.create(channelId0);
this._addListener(widget._channel0);
_channel0 = await RtcChannel.create(_channelId0);
this._addListener(_channel0);

await widget._channel0.setClientRole(ClientRole.Broadcaster);
await widget._channel0
.joinChannel(null, null, 0, ChannelMediaOptions(true, true));
await _channel0.setClientRole(ClientRole.Broadcaster);
await _channel0.joinChannel(null, null, 0, ChannelMediaOptions(true, true));
}

_joinChannel1() async {
if (defaultTargetPlatform == TargetPlatform.android) {
await [Permission.microphone, Permission.camera].request();
}

widget._channel1 = await RtcChannel.create(channelId1);
this._addListener(widget._channel1);
_channel1 = await RtcChannel.create(_channelId1);
this._addListener(_channel1);

await widget._channel1.setClientRole(ClientRole.Broadcaster);
await widget._channel1
.joinChannel(null, null, 0, ChannelMediaOptions(true, true));
await _channel1.setClientRole(ClientRole.Broadcaster);
await _channel1.joinChannel(null, null, 0, ChannelMediaOptions(true, true));
}

_addListener(RtcChannel channel) {
String channelId = channel.channelId;
channel.setEventHandler(
RtcChannelEventHandler(joinChannelSuccess: (channel, uid, elapsed) {
log('joinChannelSuccess ${channel} ${uid} ${elapsed}');
if (channelId == channelId0) {
if (channelId == _channelId0) {
setState(() {
isJoined0 = true;
});
} else if (channelId == channelId1) {
} else if (channelId == _channelId1) {
setState(() {
isJoined1 = true;
});
Expand All @@ -95,12 +91,12 @@ class _State extends State<MultiChannel> {
log('userOffline ${channel.channelId} $uid $reason');
}, leaveChannel: (stats) {
log('leaveChannel ${channel.channelId} ${stats.toJson()}');
if (channelId == channelId0) {
if (channelId == _channelId0) {
this.setState(() {
isJoined0 = false;
remoteUid0.clear();
});
} else if (channelId == channelId1) {
} else if (channelId == _channelId1) {
this.setState(() {
isJoined1 = false;
remoteUid1.clear();
Expand All @@ -109,21 +105,21 @@ class _State extends State<MultiChannel> {
}, remoteVideoStateChanged: (uid, state, reason, elapsed) {
log('remoteVideoStateChanged ${uid} ${state} ${reason} ${elapsed}');
if (state == VideoRemoteState.Starting) {
if (channelId == channelId0) {
if (channelId == _channelId0) {
this.setState(() {
remoteUid0.add(uid);
});
} else if (channelId == channelId1) {
} else if (channelId == _channelId1) {
this.setState(() {
remoteUid1.add(uid);
});
}
} else if (state == VideoRemoteState.Stopped) {
if (channelId == channelId0) {
if (channelId == _channelId0) {
this.setState(() {
remoteUid0.removeWhere((element) => element == uid);
});
} else if (channelId == channelId1) {
} else if (channelId == _channelId1) {
this.setState(() {
remoteUid1.removeWhere((element) => element == uid);
});
Expand All @@ -133,21 +129,21 @@ class _State extends State<MultiChannel> {
}

_publishChannel0() async {
await widget._channel1?.unpublish();
await widget._channel0?.publish();
await _channel1.unpublish();
await _channel0.publish();
}

_publishChannel1() async {
await widget._channel0?.unpublish();
await widget._channel1?.publish();
await _channel0.unpublish();
await _channel1.publish();
}

_leaveChannel0() async {
await widget._channel0?.leaveChannel();
await _channel0.leaveChannel();
}

_leaveChannel1() async {
await widget._channel1?.leaveChannel();
await _channel1.leaveChannel();
}

@override
Expand All @@ -160,15 +156,15 @@ class _State extends State<MultiChannel> {
children: [
Expanded(
flex: 1,
child: RaisedButton(
child: ElevatedButton(
onPressed: () {
if (isJoined0) {
this._leaveChannel0();
} else {
this._joinChannel0();
}
},
child: Text('${isJoined0 ? 'Leave' : 'Join'} $channelId0'),
child: Text('${isJoined0 ? 'Leave' : 'Join'} $_channelId0'),
),
)
],
Expand All @@ -177,15 +173,15 @@ class _State extends State<MultiChannel> {
children: [
Expanded(
flex: 1,
child: RaisedButton(
child: ElevatedButton(
onPressed: () {
if (isJoined1) {
this._leaveChannel1();
} else {
this._joinChannel1();
}
},
child: Text('${isJoined1 ? 'Leave' : 'Join'} $channelId1'),
child: Text('${isJoined1 ? 'Leave' : 'Join'} $_channelId1'),
),
)
],
Expand All @@ -198,29 +194,29 @@ class _State extends State<MultiChannel> {
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
RaisedButton(
ElevatedButton(
onPressed: this._publishChannel0,
child: Text('Publish ${channelId0}'),
child: Text('Publish ${_channelId0}'),
),
RaisedButton(
ElevatedButton(
onPressed: () {
setState(() {
renderChannelId = channelId0;
renderChannelId = _channelId0;
});
},
child: Text('Render ${channelId0}'),
child: Text('Render ${_channelId0}'),
),
RaisedButton(
ElevatedButton(
onPressed: this._publishChannel1,
child: Text('Publish ${channelId1}'),
child: Text('Publish ${_channelId1}'),
),
RaisedButton(
ElevatedButton(
onPressed: () {
setState(() {
renderChannelId = channelId1;
renderChannelId = _channelId1;
});
},
child: Text('Render ${channelId1}'),
child: Text('Render ${_channelId1}'),
),
],
),
Expand All @@ -230,10 +226,10 @@ class _State extends State<MultiChannel> {
}

_renderVideo() {
List<int> remoteUid = null;
if (renderChannelId == channelId0) {
List<int>? remoteUid = null;
if (renderChannelId == _channelId0) {
remoteUid = remoteUid0;
} else if (renderChannelId == channelId1) {
} else if (renderChannelId == _channelId1) {
remoteUid = remoteUid1;
}
return Expanded(
Expand Down
1 change: 1 addition & 0 deletions example/lib/examples/basic/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:agora_rtc_engine_example/examples/basic/join_channel_audio.dart'
import 'package:agora_rtc_engine_example/examples/basic/join_channel_video.dart';
import 'package:agora_rtc_engine_example/examples/basic/string_uid.dart';

/// Data source for basic examples
final Basic = [
{'name': 'Basic'},
{'name': 'JoinChannelAudio', 'widget': JoinChannelAudio()},
Expand Down
Loading

0 comments on commit b555e37

Please sign in to comment.