Flutter 无法在颤振中使用GlobalKey的currentState属性
我正在尝试使用Globalkey的currentstate打印文本字段的当前值,如图所示:Flutter 无法在颤振中使用GlobalKey的currentState属性,flutter,dart,Flutter,Dart,我正在尝试使用Globalkey的currentstate打印文本字段的当前值,如图所示: GlobalkeyName.currentState.value 当我在Statefull小部件中执行此操作时,我没有遇到任何问题,但当我在showModalBottomSheet中尝试执行相同操作时,我遇到了一个错误 没有为类“Key”定义getter“currentState”。 附言:我知道如何使用TextEditingController。别建议这样 完整的源代码如下 import 'pack
GlobalkeyName.currentState.value
当我在Statefull小部件中执行此操作时,我没有遇到任何问题,但当我在showModalBottomSheet中尝试执行相同操作时,我遇到了一个错误
没有为类“Key”定义getter“currentState”。
附言:我知道如何使用TextEditingController。别建议这样
完整的源代码如下
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatelessWidget(),
),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key key}) : super(key: key);
Key _coffeeformkey=GlobalKey<FormState>();
Key _nametextfield=GlobalKey<FormFieldState>();
Key _sugartextfield=GlobalKey<FormFieldState>();
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: const Text('showModalBottomSheet'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
child: Form(
key: _coffeeformkey,
child: Center(
heightFactor: 2,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
//Text("${brews.documents}"),
FractionallySizedBox(
widthFactor: 0.7,
child: TextFormField(
key: _nametextfield,
// initialValue: usertoselect1.data['name'],
//controller: namecontroller,
decoration: InputDecoration(hintText: "Enter Email"),
),
),
SizedBox(
height: 30,
),
FractionallySizedBox(
widthFactor: .7,
child: TextFormField(
key: _sugartextfield,
//controller: sugarcontroller,
//initialValue:await usertoselect1.data['sugar'],
decoration: InputDecoration(hintText: "Enter Password"),
),
),
SizedBox(
height: 30,
),
RaisedButton(
onPressed:(){
//final DatabaseService _database= DatabaseService();
print("yyfdyyyyyyyyyyyyyy");
print(_sugartextfield.currentState.value);
//print(_sugartextfield.r);
//print(_sugartextfield.runtimeType);
//_database.updateUser(_nametextfield.currentState.value, sugarcontroller.text, 400);
},
child: Text("Update"),
color: Colors.brown,
)
],
),
),
),
);
},
);
},
),
);
}
}
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
///此小部件是主应用程序小部件。
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
家:脚手架(
appBar:appBar(标题:常量文本(_title)),
正文:MyStatelessWidget(),
),
);
}
}
///这是主应用程序实例化的无状态小部件。
类MyStatelessWidget扩展了无状态小部件{
MyStatelessWidget({Key}):super(Key:Key);
Key _coffeeformkey=GlobalKey();
键_nametextfield=GlobalKey();
键_sugartextfield=GlobalKey();
@凌驾
小部件构建(构建上下文){
返回中心(
孩子:升起按钮(
子项:const Text('showModalBottomSheet'),
已按下:(){
showModalBottomSheet(
上下文:上下文,
生成器:(BuildContext上下文){
返回容器(
孩子:表格(
钥匙:咖啡钥匙,
儿童:中心(
高度系数:2,
子:列(
mainAxisAlignment:mainAxisAlignment.space,
//crossAxisAlignment:crossAxisAlignment.center,
mainAxisSize:mainAxisSize.max,
儿童:[
//文本(“${brews.documents}”),
部分溶解箱(
宽度系数:0.7,
子项:TextFormField(
键:_nametextfield,
//initialValue:usertoselect1.data['name'],
//控制器:名称控制器,
装饰:输入装饰(hintText:“输入电子邮件”),
),
),
大小盒子(
身高:30,
),
部分溶解箱(
宽度系数:.7,
子项:TextFormField(
键:_sugartextfield,
//控制员:糖控制员,
//initialValue:等待usertoselect1.data['sugar'],
装饰:输入装饰(hintText:“输入密码”),
),
),
大小盒子(
身高:30,
),
升起的按钮(
已按下:(){
//最终数据库服务_database=DatabaseService();
打印(“YYFDYYYYYYYYYY”);
打印(_sugartextfield.currentState.value);
//打印(_sugartextfield.r);
//打印(_sugartextfield.runtimeType);
//_database.updateUser(_nametextfield.currentState.value,sugarcontroller.text,400);
},
子项:文本(“更新”),
颜色:Colors.brown,
)
],
),
),
),
);
},
);
},
),
);
}
}
很显然,要这样声明密钥:
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final GlobalKey _formKey=GlobalKey();
而不是像这样为我工作:
Key _sugartextfield=GlobalKey<FormFieldState>();
Key\u sugartextfield=GlobalKey();
通过将密钥声明更改为以下代码来修复错误:
// use a global key instead
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
//改用全局键
最终的GlobalKey _formKey=GlobalKey();
注意:键
和gloableKey
之间的区别解释如下:
键
键是小部件、元素和语义节点的标识符
全局密钥
整个应用程序中唯一的密钥
要了解有关键的更多信息,请查看以下链接:
每个人都愿意解释为什么第一种方法在正常小部件(如Statefull或stateless)中有效,但对于showModalBottomSheet,我们必须使用第二种方法。