deandrehaijiel
deandrehaijiel

Reputation: 153

Flutter pass data from one statefulwidget to state

i have the code below, i would like to pass 'channelname' from [1] to [2]. how do i achieve that?

error that i am receiving 'The instance member 'channelname' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression'

class VideoCallScreen extends StatefulWidget {
  static Route get route => MaterialPageRoute(
        builder: (context) => const VideoCallScreen(),
      );
  final String? channelname; // -- [1]
  const VideoCallScreen({Key? key, this.channelname}) : super(key: key);

  @override
  State<VideoCallScreen> createState() => _VideoCallScreenState();
}

class _VideoCallScreenState extends State<VideoCallScreen> {
  bool _isMute = false;
  bool _isSwitchCamera = false;
  bool _isDisableCamera = false;

  final AgoraClient client = AgoraClient(
    agoraConnectionData: AgoraConnectionData(
        appId: AgoraConfig.appId,
        channelName: channelname), // -- [2]
    enabledPermission: [Permission.camera, Permission.microphone],
  );

@override
  void initState() {
    super.initState();
    initAgora();
  }

  void initAgora() async {
    await client.initialize();
  }

Upvotes: 0

Views: 50

Answers (1)

Ayad Salim
Ayad Salim

Reputation: 98

  • Initialize client in initState function.

  • Use late keyword to declare client.

  • Use widget.channelname to access the channelname.

    class _VideoCallScreenState extends State<VideoCallScreen> {
      bool _isMute = false;
      bool _isSwitchCamera = false;
      bool _isDisableCamera = false;
    
      late final AgoraClient client;
    
      void initState() {
        client = AgoraClient(
          agoraConnectionData: AgoraConnectionData(
            appId: AgoraConfig.appId,
            channelName: widget.channelname,
          ),
          enabledPermission: [Permission.camera, Permission.microphone],
        );
      }
    ...
    

Upvotes: 1

Related Questions