android - 无法在初始化程序中访问实例成员“setState”
问题描述
我在颤振中很新。我不知道该怎么做才能解决这个问题。
我正在尝试使用 Flutter 插件: flutter_numpad_widget
这是我的完整代码:
import 'package:flutter/material.dart';
import 'package:flutter_numpad_widget/flutter_numpad_widget.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
bool _confirmEnabled = false;
class _MyAppState extends State<MyApp> {
int maxRawLength;
final NumpadController _numpadController = NumpadController(
format: NumpadFormat.NONE,
hintText: "Ketikkan NIP",
onInputValidChange: (bool valid) => setState(() {
_confirmEnabled = valid;
}),
);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Numpad Example',
theme: ThemeData(
primarySwatch: Colors.amber,
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.normal,
buttonColor: Colors.blueGrey[300],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(30))))),
home: Scaffold(
appBar: AppBar(
title: Text('Numpad Example'),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: NumpadText(
style: TextStyle(fontSize: 30),
controller: _numpadController,
),
),
Expanded(
child: Numpad(
controller: _numpadController,
buttonTextSize: 40,
),
)
],
),
),
));
}
}
我在这里关注文档: onInputValidChange
但在这一行中,它不断让我出现错误“无法在初始化程序中访问实例成员'setState'。”:
onInputValidChange: (bool valid) => setState(() {
_confirmEnabled = valid;
}),
我已经在几天内搜索并一无所获。
感谢您的帮助
解决方案
为您的问题添加一些解释,我认为通常也是有效的:
您应该在initState
. 如果你有喜欢的布尔标志或原始属性,但对象,一般来说,你应该在```initState```中初始化。在你的情况下:
import 'package:flutter/material.dart';
import 'package:flutter_numpad_widget/flutter_numpad_widget.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
bool _confirmEnabled = false;
class _MyAppState extends State<MyApp> {
int maxRawLength;
final NumpadController _numpadController; // this is the declaration
@override
void initState() {
super.initState();
_numpadController = NumpadController( // here is the init
format: NumpadFormat.NONE,
hintText: "Ketikkan NIP",
onInputValidChange: (bool valid) => setState(() {
_confirmEnabled = valid;
}),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Numpad Example',
theme: ThemeData(
primarySwatch: Colors.amber,
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.normal,
buttonColor: Colors.blueGrey[300],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(30))))),
home: Scaffold(
appBar: AppBar(
title: Text('Numpad Example'),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: NumpadText(
style: TextStyle(fontSize: 30),
controller: _numpadController,
),
),
Expanded(
child: Numpad(
controller: _numpadController,
buttonTextSize: 40,
),
)
],
),
),
));
}
}
推荐阅读
- sql - 如何限制您选择的字符数量
- sql-server - SQL - 仅在用户进行更新时触发插入
- c# - 有没有办法可以使用 PictureBox MouseMove 事件但仍然在整个屏幕上移动?
- javascript - Append() 函数在 chrome 中工作,但在 Firefox 中不工作,层次结构错误
- vue.js - Vuejs 观察者对道具的变化没有反应
- python - 如何安装模块名称 pb?
- hp-uft - 如何让 HP ALM 在 Windows 10 工作站/主机上远程运行 HP UFT 脚本
- swift - Swift 文本字段边框不是正确的宽度
- linux - 使用循环将数据从两行打印到另一个文件
- sql - 当我的所有时间戳都是 UTC 时,如何查询“昨天,在我的时区”