Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LateError (LateInitializationError: Field 'requestPort' has not been initialized [BUG] #113

Closed
mohamedELamine opened this issue Dec 10, 2022 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@mohamedELamine
Copy link

LateError (LateInitializationError: Field 'requestPort' has not been initialized
when i try this code
`import 'dart:async';

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:flutter/material.dart';

const String appId = "756d0451e027460dbb9212a2f46c141a";
void main() {
runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({super.key});

@OverRide
State createState() => _MyAppState();
}

class _MyAppState extends State {
late RtcEngine agoraEngine; // Agora engine instance
String channelName = "ARyhan";
final GlobalKey scaffoldMessengerKey =
GlobalKey(); // Global key to access the scaffold

String token = "9088f484799548efba2ac5f987c63df9";
int uid = 0; // uid of the local user

bool _isHost =
true; // Indicates whether the user has joined as a host or audience

bool _isJoined = false; // Indicates if the local user has joined the channel
int? _remoteUid; // uid of the remote user

@OverRide
void dispose() async {
await agoraEngine.leaveChannel();
super.dispose();
}

@OverRide
void initState() {
super.initState();
setupVideoSDKEngine();
}

showMessage(String message) {
scaffoldMessengerKey.currentState?.showSnackBar(SnackBar(
content: Text(message),
));
}

Future setupVideoSDKEngine() async {
// retrieve or request camera and microphone permissions
await [Permission.microphone, Permission.camera].request();

//create an instance of the Agora engine
agoraEngine = createAgoraRtcEngine();
await agoraEngine.initialize(const RtcEngineContext(appId: appId));

await agoraEngine.enableVideo();

// Register the event handler
agoraEngine.registerEventHandler(
  RtcEngineEventHandler(
    onJoinChannelSuccess: (RtcConnection connection, int elapsed) {
      showMessage(
          "Local user uid:${connection.localUid} joined the channel");
      setState(() {
        _isJoined = true;
      });
    },
    onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) {
      showMessage("Remote user uid:$remoteUid joined the channel");
      setState(() {
        _remoteUid = remoteUid;
      });
    },
    onUserOffline: (RtcConnection connection, int remoteUid,
        UserOfflineReasonType reason) {
      showMessage("Remote user uid:$remoteUid left the channel");
      setState(() {
        _remoteUid = null;
      });
    },
  ),
);

}

void join() async {
// Set channel options
ChannelMediaOptions options;

// Set channel profile and client role
if (_isHost) {
  options = const ChannelMediaOptions(
    clientRoleType: ClientRoleType.clientRoleBroadcaster,
    channelProfile: ChannelProfileType.channelProfileLiveBroadcasting,
  );
  await agoraEngine.startPreview();
} else {
  options = const ChannelMediaOptions(
    clientRoleType: ClientRoleType.clientRoleAudience,
    channelProfile: ChannelProfileType.channelProfileLiveBroadcasting,
  );
}

await agoraEngine.joinChannel(
  token: token,
  channelId: channelName,
  options: options,
  uid: uid,
);

}

void leave() {
setState(() {
_isJoined = false;
_remoteUid = null;
});
agoraEngine.leaveChannel();
}

Widget _videoPanel() {
if (!_isJoined) {
return const Text(
'Join a channel',
textAlign: TextAlign.center,
);
} else if (_isHost) {
// Local user joined as a host
return AgoraVideoView(
controller: VideoViewController(
rtcEngine: agoraEngine,
canvas: VideoCanvas(uid: uid),
),
);
} else {
// Local user joined as audience
if (_remoteUid != null) {
return AgoraVideoView(
controller: VideoViewController.remote(
rtcEngine: agoraEngine,
canvas: VideoCanvas(uid: _remoteUid),
connection: RtcConnection(channelId: channelName),
),
);
} else {
return const Text(
'Waiting for a host to join',
textAlign: TextAlign.center,
);
}
}
}

// Set the client role when a radio button is selected
void _handleRadioValueChange(bool? value) async {
setState(() {
_isHost = (value == true);
});
if (_isJoined) leave();
}

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
scaffoldMessengerKey: scaffoldMessengerKey,
home: Scaffold(
appBar: AppBar(
title: const Text('Get started with Interactive Live Streaming'),
),
body: ListView(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
children: [
// Container for the local video
Container(
height: 240,
decoration: BoxDecoration(border: Border.all()),
child: Center(child: _videoPanel()),
),
// Radio Buttons
Row(children: [
Radio(
value: true,
groupValue: _isHost,
onChanged: (value) => _handleRadioValueChange(value),
),
const Text('Host'),
Radio(
value: false,
groupValue: _isHost,
onChanged: (value) => _handleRadioValueChange(value),
),
const Text('Audience'),
]),
// Button Row
Row(
children: [
Expanded(
child: ElevatedButton(
child: const Text("Join"),
onPressed: () => {join()},
),
),
const SizedBox(width: 10),
Expanded(
child: ElevatedButton(
child: const Text("Leave"),
onPressed: () => {leave()},
),
),
],
),
// Button Row ends
],
)),
);
}
}
`

@mohamedELamine mohamedELamine added the bug Something isn't working label Dec 10, 2022
@maxxfrazer
Copy link
Contributor

@mohamedELamine please you use the issue template provided with the GitHub repo.

@jabguru
Copy link

jabguru commented Jan 9, 2023

Yeah I encountered this same bug after upgrading to the latest version @maxxfrazer @mohamedELamine @Meherdeep @Ascenio

LateError (LateInitializationError: Field 'requestPort' has not been initialized

@Meherdeep
Copy link
Contributor

@jabguru Looks like you're using the agora_rtc_engine package and not the UIKit.

However, the error suggests that you shouldn't be using late instead you should be using a nullable variable.

@jabguru
Copy link

jabguru commented Jan 11, 2023

Yeah, fixed it, thanks.

@faisalmushtaq007
Copy link

i am getting the same issue with agora ui kit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants