如何在 flutter 中使用 switch 语句在侧导航栏上执行注销?

How to perform signOut on sidenavbar using switch statement in flutter?

我是 flutter 的新手,我正在尝试在 sidenavbar 上执行 signOut() 函数。我陷入了代码段。几次尝试后,我收到如下错误:

type Future is not a subtype of type Widget

我有以下代码。 在我的案例 0: 语句中,我应该如何调用 _logOutUser() 函数? 任何帮助将不胜感激

final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

class Homepage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      routes: {
        '/login': (context) => Login(),
      },
      title: 'NavigationDrawer Demo',
      theme: new ThemeData(
        primarySwatch: Colors.red,
      ),
      home: new HomePage(),
    );
  }
}

class DrawerItem {
  String title;
  IconData icon;
  DrawerItem(this.title, this.icon);
}

class HomePage extends StatefulWidget {
  HomePage({
    this.auth,
    this.onSignedOut,
  });
  final AuthImplementation auth;
  final VoidCallback onSignedOut;
  final drawerItems = [
    new DrawerItem("Logout", Icons.exit_to_app),
  ];

  @override
  State<StatefulWidget> createState() {
    return new HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  void _logOutUser() async {
    try {
      await widget.auth.signOut();
      widget.onSignedOut();
    } catch (e) {
      print(e.toString());
    }
  }

  int _selectedDrawerIndex = 0;
  _getDrawerItemWidget(int pos) {
    switch (pos) {
      case 0:
        return HomePageState()._logOutUser();
      default:
        return new Text("Error");
    }
  }

  _onSelectItem(int index) {
    setState(() => _selectedDrawerIndex = index);
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    var drawerOptions = <Widget>[];
    for (var i = 0; i < widget.drawerItems.length; i++) {
      var d = widget.drawerItems[i];
      drawerOptions.add(
          new ListTile(
            leading: new Icon(d.icon),
            title: new Text(d.title),
            selected: i == _selectedDrawerIndex,
            onTap: () => _onSelectItem(i),
          )
      );
    }

  }
}

我对你的问题有点困惑,但如果我没记错的话,你为什么不这样做:

switch (pos) {
      case 0:
        HomePageState()._logOutUser();
        return Text("Logged Out");
        // or if you don't want to show anything you could just return a blank Container, or
        // if you wanted to navigate to login screen just use Navigator.push() and return the Container
      default:
        return new Text("Error");
    }

您收到错误 type Future<dynamic> is not a subtype of Widget 的原因是您的函数 _logOutUser() 没有返回 Widget。如果我没有回答你的问题,请在下方评论。