The method 'UserUid' isn't defined for the type '_HomeState'

I’m developing a Flutter mobile app. I’m doing the authentication using Firebase, but I have a problem. I want to add a button to log out, but I’m facing this error, how can I solve it? I need help.
UserUid(), OutButton(), I am getting error in these two places.With the help of Firebase, I wanted to take the current user and log out with the signOut method, but I wanted to add it to a button in the method and add it as a widget, but I could not solve the error.

Errors: The method ‘UserUid’ isn’t defined for the type ‘_HomeState’.
Errors: The method ‘OutButton’ isn’t defined for the type ‘_HomeState’.

I think it is because I use statefull widget but I have to use statefull and I definitely need to add a exit button to this page.

class Home extends StatefulWidget {
 static String routeName="/home";
 Home({Key? key}) : super(key: key);
 final User? user = Auth().currentUser;

 Future<void> signOut() async {
   await Auth().signOut();
 }

 Widget Title_() {
   return const Text('Firebase Auth');
 }

 Widget UserUid() {
   return Text(user?.email ?? 'User email');
 }

 Widget OutButton() {
   return ElevatedButton(
     onPressed: signOut,
     child: const Text('Sign Out'),
   );
 }


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

class _HomeState extends State<Home> {
 String getEmoji(String text) {
   if (text == "anger") {
     return '😠️ 💢';
   } else if (text == "joy") {
     return "😂 😭 🤣";
   } else if (text == "sadness") {
     return "😥 😔 😓";
   } else if (text == "fear") {
     return "😨 😱";
   } else if (text == "love") {
     return "❤ 💕 🥰";
   } else if (text == "surprise") {
     return "😯 😮 😲";
   } else {
     return "❓";
   }
 }
 String url = '';
 var data='';
 String output = 'Initial Output';
 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(title: Text('Emoji Suggester')),
     body: Center(
       child: Container(
         padding: EdgeInsets.all(20),
         child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
           TextField(
             onChanged: (value) {
               url = 'http://10.0.2.2:5000/emotions?text=' + value;
             },
           ),
           TextButton(
               onPressed: () async {
                 data = await fetchdata(url);
                 setState(() {
                   output = data;
                 });
               },
               child: Text(
                 'Emotions Suggester',
                 style: TextStyle(fontSize: 20),
               )),
           Text(
             getEmoji(data)+data,
             style: TextStyle(fontSize: 25, color: Colors.green),
           ),
           UserUid(),
           OutButton(),
         ]
         ),
       ),
     ),
   );
 }
}

>Solution :

If you want to access something from your StatefulWidget class in your state class, you need to use widget. like this:

Text(
   getEmoji(data)+data,
   style: TextStyle(fontSize: 25, color: Colors.green),
),
widget.UserUid(), // <=== change this
OutButton(),

Leave a Reply