Flutter 获取WidgetsBindingObserver的小部件观察者
我正在尝试将WidgetsBindingObserver帐户添加到()Flutter 获取WidgetsBindingObserver的小部件观察者,flutter,dart,Flutter,Dart,我正在尝试将WidgetsBindingObserver帐户添加到() 类MyApp使用WidgetsBindingObserver扩展StatefulWidget{ @凌驾 _MyAppState createState()=>\u MyAppState(); } 类MyAppState扩展了状态{ @凌驾 void initState(){ super.initState(); WidgetsBinding.instance.addObserver(this);//错误就在这里 } @凌驾
类MyApp使用WidgetsBindingObserver扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(this);//错误就在这里
}
@凌驾
无效处置(){
WidgetsBinding.instance.removeObserver(此);
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回MaterialHomeApp();
}
错误说明
参数类型“\u MyAppState”无法分配给参数类型“WidgetsBindingObserver”。dartargument\u type\u不可分配
您可以使用WidgetsBindingObserver将从MyApp
移动到\u MyAppState
您可以在这里参考官方文件
代码片段
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类_MyAppState使用WidgetsBindingObserver扩展状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
完整示例代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类_MyAppState使用WidgetsBindingObserver扩展状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
@凌驾
无效处置(){
WidgetsBinding.instance.removeObserver(此);
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
风格:Theme.of(context).textTheme.headline4,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}
类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类_MyAppState使用WidgetsBindingObserver扩展状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
@凌驾
无效处置(){
WidgetsBinding.instance.removeObserver(此);
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回MaterialHomeApp();
}
在代码中更改上述内容
WidgetsBindingObserver是注册Widgets层绑定的类的接口。必须如上所示声明它。请按如上所示对代码进行必要的更改。在SO中只编写代码答案而不进行任何描述不是一个好的做法。@Themisr,感谢您的建议。我已经对我的ans进行了更改wer.@AbdullahTh,请求你帮个忙。你能选择我的答案吗?这和chunhunghan给出的答案是一样的。我已经从我的声望点为这个问题设置了赏金+100,我想要回来。所以,请帮我个忙。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialHomeApp();
}