在其状态类中访问有状态窗口小部件的功能?扑 [英] Accessing a function of stateful widget in its state class? flutter
问题描述
我试图在onPressed方法退出按钮后调用_signOut函数.但是它没有(识别函数或让我调用它),但是我可以调用call widget.Onsignedout,它的父级回调,一切都按预期工作.除了我在auth.signout上注销用户,然后回叫只是为了更新表单.如何从状态类访问_signOut()?谢谢
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),
),
),
),
);
}
}
推荐答案
您需要将方法公开.现在,由于名称前有下划线(_
),该方法是私有的,您无法访问它.
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.
只需将名称从_signOut
更改为signOut
,然后您就可以通过widget.signOut()
调用它.
Simply change the name from _signOut
to signOut
and then you should be able to call it by widget.signOut()
.
这篇关于在其状态类中访问有状态窗口小部件的功能?扑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!