Home > Software design >  "LateInitializationError: Field '_client@64280004' has not been initialized."
"LateInitializationError: Field '_client@64280004' has not been initialized."

Time:01-14

late AgoraClient _client;

This is how I initialized the agora Client with a late modifier.

Below is the AgoraVideoViewer

children: [
            AgoraVideoViewer(
              client: _client
            ),
            AgoraVideoButtons(
              client: _client
            )
          ],

I tried nulling the AgoraClient as shown below

AgoraClient? _client;
children: [
    AgoraVideoViewer(
      client: _client!
    ),
    AgoraVideoButtons(
      client: _client!
    )
],

This returned "Null check operator used on a null value" and proceeded to the call screen but displaying a white screen

Future<void> _initAgora() async {
await _client.initialize();
String link = "https://agoratokenserver.rich670.repl.co/access_token?channelName=${widget.channelName}";
Response response = await get(Uri.parse(link));
Map data = jsonDecode(response.body);

setState(() async {
  token = data["token"];
});
_client = AgoraClient(
  agoraConnectionData: AgoraConnectionData(
    appId: "fa3ebd1e9f854c9c8a60430b5fe4d64c",
    tempToken: token,
    channelName: widget.channelName,
  ),
  enabledPermission: [Permission.camera, Permission.microphone],
);
Future.delayed(const Duration(seconds: 1)).then((value) => setState(() => isLoading = false),);}

Take a look at my constructor _initAgora();

CodePudding user response:

I believe you should try using question mark '?' instead of '!' sign.

CodePudding user response:

This is how i solved it

late AgoraClient client = AgoraClient(
  agoraConnectionData: AgoraConnectionData(
    appId: "fa3ebd1e9f854c9c8a60430b5fe4d64c",
    tempToken: token,
    channelName: widget.channelName,
  ),
  // agoraChannelData: AgoraChannelData(
  //   channelProfile: ChannelProfile.LiveBroadcasting,
  //   clientRole: ClientRole.Audience,
  // ),
  enabledPermission: [Permission.camera, Permission.microphone],
);

I changed initialization of the 'AgoraClient' from the constructor to the initializer. At first it had an error on 'token' and 'channelName' but then I labelled the AgoraClient as 'late'

Check the block of code above

  • Related