我试图获取 TextFormField 值。但结果是 null
主页,
children:[
UrlTextField(),
UsernameTextField(),
UrlButton()
]
UrlTextField(),同UsernameTextField()
class UrlTextField extends StatelessWidget {
final myController = TextEditingController();
@override
Widget build(BuildContext context) {
return AppTextField(
decoration:
InputDecoration(prefixText: "https://", labelText: "Enter your URL"),
myController: myController,
textInputType: TextInputType.url,
);}}
AppTextField() 很普通的类,我到处都用到这个类
class AppTextField extends StatelessWidget {
final InputDecoration decoration;
var myController = TextEditingController();
final TextInputType textInputType;
AppTextField({
this.decoration,
this.myController,
this.textInputType
});
@override
Widget build(BuildContext context) {
return TextFormField(
controller: myController,
keyboardType: textInputType,
textAlign: TextAlign.left,
decoration: decoration
);}}
我需要在单击按钮或任何其他区域时获取 Url 和用户名值,
class UrlButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AppButton(
onPressed: () {
String url = UrlTextField().myController.text;
String username = UsernameTextField().myController.text;
print('url is $text');
});}}
AppButton() 这个类也很常见
class AppButton extends StatelessWidget {
final VoidCallback onPressed;
AppButton({
this.buttonTextStyle
});
@override
Widget build(BuildContext context) {
return RaisedButton(
child: Text(...),
onPressed: onPressed);}}
最佳答案
您正在尝试从刚刚在按钮的 onPressed 中实例化的 Controller 中检索文本,因此到目前为止还没有任何文本!要解决此问题,您需要某种状态管理方式来访问和更改现有小部件,在您的例子中是 UrlTextField 小部件。我将举例说明如何快速解决此问题:
主页:
class MainPage extends StatefulWidget {
...
@override
createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
UrlTextField _urlTextField = UrlTextField();
...
children:[
_urlTextField,
UsernameTextField(),
UrlButton(_urlTextField)
]
现在我们实例化了一个 UrlTextField,它可以被引用并可以传递给另一个小部件,例如您的 UrlButton:
class UrlButton extends StatelessWidget {
final UrlTextField urlTextField;
UrlButton(this.urlTextField);
@override
Widget build(BuildContext context) {
return AppButton(
onPressed: () {
String url = this.urlTextField.myController.text;
String username = UsernameTextField().myController.text;
print('url is $text');
}
);
}
}
通过这种方式,您实例化了一个 UrlTextField 并在您的主页中使用它,用户可以在其中填写一些输入并将其传递给 UrlButton,您可以在其中访问它的 Controller 及其文本。
我建议您更多地研究状态管理主题,因为有很多方法可以处理这种情况。我可以推荐你看一下 Provider,它非常易于使用并且可以方便地访问某些数据。
关于flutter - TextFormField 值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473504/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我正在尝试从googleAPI下载client_secret.json。我正在执行https://developers.google.com/gmail/api/quickstart/ruby中列出的步骤.使用此向导在GoogleDevelopersConsole中创建或选择项目并自动启用API。在左侧边栏中,选择同意屏幕。选择电子邮件地址并输入产品名称(如果尚未设置),然后单击“保存”按钮。在左侧边栏中,选择凭据并点击创建新客户端ID。选择应用程序类型已安装应用程序,已安装应用程序类型为其他,然后单击“创建客户端ID”按钮。点击新客户端ID下的下载JSON按钮。将此文件移动到您的工作
我正在使用pggem从Ruby与PostgreSQL对话。有没有检查是否没有结果比使用res.ntuples==0更好的方法?conn=PGconn.connectconfigcmd="select*fromlabelsinnerjoinlabels_mailusing(label_id)"+"wherelabels_mail.mail_id=$1andlabels.name=$2"res=conn.exec(cmd,[mail_id,mailbox])ifres.ntuples==0# 最佳答案 元组,打字错误?使用zero?比=
问题localhost:3000/users/不会显示我谦虚地进入,因为我是第一次尝试通过Rails教程。我在第10章,我已经花了5个小时解决这个问题。当我尝试访问localhost:3000/users/时出现错误(我相信这与factory_girl有关)解释了@users变量为空并且我忘记了为will_paginate传递一个集合对象。我目前在第10章第10.23节,每次运行时:$bundleexecrakedb:reset$bundleexecrakedb:populate$bundleexecrakedb:test:prepare我在解释时遇到错误rakeaborted!Fac
一、环境变量右键点击我的电脑-属性:然后找到环境变量 1.Android的SDK不在C盘的话需要额外配这个到用户环境变量:ANDROID_HOMED:\AndroidSDK2.然后在系统变量:Path中添加一条这样的值 D:\Flutter\flutter\bin 这个值写flutter包解压的实际地址即可 3.在系统变量中添加两个镜像变量: 变量名:FLUTTER_STORAGE_BASE_URL 变量值:https://storage.flutter-io.cn 变量名:PUB_HOSTED_URL 变量
我是Rails新手。得到下面的错误。我了解问题所在,但不确定如何解决?错误-参数缺失或值为空:客户defcustomer_paramsparams.require(:customer).permit(:first_name,:last_name,:email,:password,:password_confirmation)endendDetailsController.rbclassMy::Account::DetailsController:showelseredirect_tomy_account_details_path,:notice=>'Accountdetailsupda
如果params[:date]的参数为空,我希望我的text_field_tag将当前日期作为默认值,这是我目前的代码:我想要类似:谢谢 最佳答案 您可以简单地使用“或”运算符。如果params[:end]为空,它将使用Time.now。 关于ruby-on-rails-如果参数为空,则Text_field_tag默认值?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/35520
我的演示.rb:putsARGV.sizeARGV.eachdo|a|puts"Argument:#{a}"end结果取决于我们如何运行脚本:>demo.rbfoobar0>rubydemo.rbfoobar2Argument:fooArgument:bar为什么会这样?可以用这个做点什么吗?编辑:感谢所有回复!这是我的设置:>assoc.rb.rb=rbFile>ftyperbFilerbFile="c:\ruby-1.8.6\bin\ruby.exe""%1"%*所以看起来是对的。但是我发现了>demo.rbfoobar使用这样的命令行启动进程:"C:\ruby-1.8.7\bin
我在Jekyll项目中有一系列帖子,其中一些只有标题,一些有标题和内容。我想在每种情况下对帖子做不同的事情。例如:{%forpostinsite.categories.publications%}{{post.title}}{%ifpost.content==""orpost.content==nilorpost.content==blank%}Nothinghere.{%else%}{{post.content}}{%endif%}{%endfor%}但是if语句实际上并没有捕获空帖子。我的条件基于thispage,但是这3种可能性都没有捕捉到空帖子。关于如何处理这些帖子有什么想法吗
我有一段代码,我想在不运行代码块内部的情况下测试正文是否为空。这可能吗? 最佳答案 sourcifygem添加了一个Proc#to_source方法:>>require'sourcify'=>true>>p=Proc.new{}=>#>>p.to_source=>"proc{}"一旦将block作为字符串,就很容易看出花括号之间是否有注释(或只有空格)。 关于ruby-如何测试一个block是否为空?,我们在StackOverflow上找到一个类似的问题: h