Home > OS >  Flutter Error: Superclass has no method named 'initState'
Flutter Error: Superclass has no method named 'initState'

Time:07-21

I am trying to implement a video calling app in Flutter using the following tutorial https://www.100ms.live/blog/flutter-webrtc#rendering-a-local-user.

However, when I run, I get the following errors

lib/main.dart(59,11): error GE5905412: Superclass has no method named 'initState'. [D:---\webrtc_flutter\build\windows\flutter\flutter_assemble.vcxproj]

lib/main.dart(65,11): error GE5905412: Superclass has no method named 'dispose'. [D:---\webrtc_flutter\build\windows\flutter\flutter_assemble.vcxproj]

Below is the code written:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final _localVideoRenderer =  RTCVideoRenderer();

  void initRenderers() async {
    await _localVideoRenderer.initialize();
  }

  _getUserMedia() async {
    final Map<String, dynamic> mediaConstraints = {
      'audio': true,
      'video': {
        'facingMode': 'user',
      }
    };

    MediaStream stream =
    await navigator.mediaDevices.getUserMedia(mediaConstraints);
    _localVideoRenderer.srcObject = stream;
  }

  @override
  void initState() {
    initRenderers();
    _getUserMedia();
    super.initState();
  }

  @override
  void dispose() async {
    await _localVideoRenderer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
      ),
      body: Stack(
        children: [
          Positioned(
              top: 0.0,
              right: 0.0,
              left: 0.0,
              bottom: 0.0,
              child: RTCVideoView(_localVideoRenderer))
        ],
      ),
    );
  }


  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

CodePudding user response:

Change your Code

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  final _localVideoRenderer =  RTCVideoRenderer();

  void initRenderers() async {
    await _localVideoRenderer.initialize();
  }

  _getUserMedia() async {
    final Map<String, dynamic> mediaConstraints = {
      'audio': true,
      'video': {
        'facingMode': 'user',
      }
    };

    MediaStream stream =
    await navigator.mediaDevices.getUserMedia(mediaConstraints);
    _localVideoRenderer.srcObject = stream;
  }

  @override
  void initState() {
    initRenderers();
    _getUserMedia();
    super.initState();
  }

  @override
  void dispose() async {
    await _localVideoRenderer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
      ),
      body: Stack(
        children: [
          Positioned(
              top: 0.0,
              right: 0.0,
              left: 0.0,
              bottom: 0.0,
              child: RTCVideoView(_localVideoRenderer))
        ],
      ),
    );
  }
}

CodePudding user response:

The functions "initState", "dispose" and "build" should be in your class "_MyHomePageState" not in "MyHomePage" as theses functions are implemented in the State class, not StatefulWidget class.

  • Related