Teg*_*007 5 widget dart flutter statefulwidget
我试图在注销按钮的 onPressed 方法之后调用 _signOut 函数。但是它没有(识别函数或让我调用它)但是我可以进行调用 widget.Onsignedout,对它的父级回调,一切都按预期工作。除了我在 auth.signout 上注销用户并回电只是为了更新表单。如何从状态类访问 _signOut()?谢谢你
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),
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
您需要公开您的方法。现在由于_名称前有下划线 ( ),该方法是私有的,您无法访问它。
只需将名称从 更改_signOut为signOut,然后您应该可以通过 调用它widget.signOut()。
| 归档时间: |
|
| 查看次数: |
7605 次 |
| 最近记录: |