5

I am trying to call _signOut function after the onPressed method for logout button. However it doesnt (recognize the function or let me call it) I can however make a call widget.Onsignedout, a callback to it parent and everything works as intended. except i sign out the user at auth.signout and call back is just to update the form. how can I access the _signOut() from state class? thank you

import 'package:flutter/material.dart';
import 'package:login_demo/auth.dart';
import 'package:login_demo/root_page.dart';

class HomePage extends StatefulWidget {
  HomePage({this.auth, this.onSignedOut});
  final BaseAuth auth;
  //To call a function of a parent, you can use the callback pattern
  final VoidCallback onSignedOut;

  void _signOut() async {
    try {
      Text('Signing Out here');
      await auth.signOut();
      onSignedOut;
    } catch (e) {
      print(e);
    }
  }

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

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Homepage'),
        actions: <Widget>[
          FlatButton(
            child: Text(
              'Logout',
              style: TextStyle(fontSize: 17.0, color: Colors.white)
            ),
            onPressed: widget.onSignedOut,
          ),
        ],
      ),
      body: Container(
        child: Center(
          child: Text(
            'Welcome',
            style: TextStyle(fontSize: 32.0),
          ),
        ),
      ),
    );
  }
}
stevenspiel
  • 5,775
  • 13
  • 60
  • 89
Tegster007
  • 91
  • 2
  • 8

2 Answers2

5

You need to make your method public. Right now because of the underscore (_) before the name, the method is private and you cannot access it.

Simply change the name from _signOut to signOut and then you should be able to call it by widget.signOut().

Marcin Szałek
  • 4,609
  • 5
  • 30
  • 43
  • making the method public didn't seem to work, I get the error. "Expression here is a type of 'void' and therefore cannot be used. If I add "auth.signOut();" to the callback onSignedout method. it seems to work. – Tegster007 Aug 07 '18 at 20:14
  • If you are doing `onPressed: widget.signOut` try changing it to `onPressed: () => widget.signOut()` and tell me if it works :) Also what you can do is remove `void` from `signOut()` method since it will return a `Future` – Marcin Szałek Aug 08 '18 at 08:00
  • 1
    I actually ended up moving the function down stateful state section and passing the two parameters through the stateful constructor. I read somewhere it was good practice. – Tegster007 Aug 20 '18 at 16:46
0

First, change your _signOut to signOut so it can be accessed by another file. OR more simple solution, move your function inside the state of your homepagestate statefulWidget.

  • Do not copy-paste someone answer – Rishabh Deep Singh Dec 22 '21 at 20:02
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '21 at 20:03