【问题标题】:how to update the Text widget value if user enter the value in Textfield如果用户在文本字段中输入值,如何更新文本小部件值
【发布时间】:2026-01-23 08:35:01
【问题描述】:

当用户在文本字段中输入文本时,我想更新文本小部件的值,这就是我正在做的事情。

String getemail="";
 _userDetails() async{
    SharedPreferences myPrefs=await SharedPreferences.getInstance();
    setState(() {
     
      getemail=myPrefs.getString('email'); 
 }); 
   }

 TextEditingController email=new TextEditingController();
Column(children: <Widget>[
             TextField(
             
             onChanged: (value) {
               setState(() {
                 valueText = value;
               });
             },
             controller: email,
             decoration: InputDecoration(hintText: getemail,icon: Icon(Icons.email)),
           ),
Container(
      padding: EdgeInsets.fromLTRB(30, 260, 0, 0),
      child: Row(children: <Widget>[
        Icon(Icons.email,color: Colors.blue,size: 27,),
        
        Text(email.text==""?getemail:email.text,style:TextStyle(fontSize: profileFontsize))
      ],),
    ),

它没有在文本小部件上打印任何内容,请帮助如何做到这一点。

更新:

奇怪!这个email.text==""?getemail:email.text 条件以前不起作用,但现在它起作用了!!

【问题讨论】:

    标签: flutter text controller textfield


    【解决方案1】:
    Text(email.text == " " ? getemail : valueText, 
         style:TextStyle(fontSize: profileFontsize),
        )
           
    

    【讨论】:

    • 发送此错误A non-null String must be provided to a Text widget. 'package:flutter/src/widgets/text.dart': Failed assertion: line 378 pos 10: 'data != null'
    • 获取电子邮件返回空值。
    • 但我在 initState() 函数中打印它有一个值!