Home > Enterprise >  The method 'initState' isn't defined in a superclass of 'ChatRoom'
The method 'initState' isn't defined in a superclass of 'ChatRoom'

Time:03-22

Im creating a chat using flutter firebase. I want to record audio audio when I click the "mic" icon. but I'm getting these errors in my code. message sending is already working. now I want send voice messages and store them in firebase. I refer this video for the implementation. for your reference you can find my full code files. voice recording codes are highlighted. >> methods.dart , chatRoom.dart , serach.dart. think Im placing the code incorrectly. apriciate your hep on this.

.

The method 'initState' isn't defined in a superclass of 'ChatRoom'.
The method 'dispose' isn't defined in a superclass of 'ChatRoom'
The method 'setState' isn't defined for the type 'ChatRoom'.

.

chatRoom.dart
     bool show = false;
      FocusNode forcusNode = FocusNode();
      **final recorder = SoundRecorder();
    
      @override
      void initState(){
        super.initState();
        recorder.init();
      }
    
      @override
      void dispose(){
        super.dispose();
        recorder.dispose();
      }**
    
      @override
      Widget build(BuildContext context) {
        final size = MediaQuery.of(context).size;
    
    
        **final isRecording = recorder.isRecording;
        final icon = isRecording ? Icons.stop: Icons.mic;
        final text = isRecording? 'STOP': 'START';
        final primary= isRecording? Colors.red : Colors.white;
        final onPrimary = isRecording ? Colors.white : Colors.black;**
    
    
        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
            title: StreamBuilder<DocumentSnapshot<Map<String ,dynamic>>>(
              stream: _firestore.collection('Users').doc(userMap['uid']).snapshots(),
              builder:(context ,snapshot){
               // print(snapshot.data!['name']);
                if(snapshot.data!.exists){
                  // print(snapshot.data!['status']);
                  return Container(
                    child: Column(
                      children: [
                             Text(userMap['name']),
                            // Text(snapshot.data!['status']),
                      ],
                    ),
                  );
                  }else{
                    return Container(child: Text(userMap['name']),);
                  }
                
                
              } ,)
            
           
          ),
          body: SingleChildScrollView(
            child: Column(children: [
              Container(
                  height: size.height / 1.25,
                  width: size.width,
                  child: StreamBuilder<QuerySnapshot>(
                    stream: _firestore
                        .collection('chatroom')
                        .doc(ChatRoomID)
                        .collection('chats')
                        .orderBy("time", descending: false)
                        .snapshots(),
                    builder: ((BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (snapshot.data != null) {
                        return ListView.builder(
                            itemCount: snapshot.data!.docs.length,
                            itemBuilder: (context, index) {
                              Map<String, dynamic>? map = snapshot.data!.docs[index]
                                  .data() as Map<String, dynamic>?;
                              return message(size, map!);
                            });
                      }
                      return Container();
                    }),
                  )),
              Container(
                height: size.height / 10,
                width: size.width,
                alignment: Alignment.center,
                child: Center(
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(5.0),
                          child: SizedBox(
                            height: size.height / 10,
                            width: size.width / 1.5,
                            child: Card(
                              margin: EdgeInsets.only(left: 2, right: 2, bottom: 8),
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(25)),
                              child: TextField(
                                focusNode: forcusNode,
                                controller: _message,
                                keyboardType: TextInputType.multiline,
                                maxLines: 5,
                                minLines: 1,
                                textAlignVertical: TextAlignVertical.center,
                                decoration: InputDecoration(
                                  border: InputBorder.none,
                                  prefixIcon: IconButton(
                                      onPressed: () {
                                        forcusNode.unfocus();
                                        forcusNode.canRequestFocus = false;
                                        show = !show;
                                      },
                                      icon: Icon(
                                        Icons.emoji_emotions,
                                        color: textGrey,
                                      )),
                                  suffixIcon: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        IconButton(
                                            onPressed: () {
                                              getImage();
                                            },
                                            icon: Icon(
                                              Icons.attach_file,
                                              color: textGrey,
                                            )),
                                        IconButton(
                                            onPressed: () {},
                                            icon: Icon(
                                              Icons.camera_alt,
                                              color: textGrey,
                                            )),
    
                                      ]),
                                  hintText: 'Type Message',
                                ),
                              ),
                            ),
                          ),
                        ),
    
                        **IconButton(
                              onPressed: () async{
                               final isRecording = await recorder.toggleRecording();
                               setState((){});
                              },
                              icon: Icon(
                                Icons.mic,
                                color: mainGreen,
                              )),**
    
                        IconButton(
                            onPressed: () {
                              onSendMessage();
                              print(_message.text);
                            },
                            icon: Icon(
                              Icons.send,
                              color: mainGreen,
                            )),
    
    
                      ]),
                ),
              ),
            ]),
          ),
        );
      }

  @override
  void dispose(){
    super.dispose();
    recorder.dispose();
  }

IconButton(
                          onPressed: () async{
                           final isRecording = await recorder.toggleRecording();
                           setState((){});
                          },
                          icon: Icon(
                            Icons.mic,
                            color: mainGreen,
                          )),

CodePudding user response:

Are you sure your ChatRoom component is a StatefulWidget?

CodePudding user response:

Since ChatRoom is a StatelessWidget, it does not have access to the initState method, since there is not state to initialize.

  • Related