Flutter更新FutureBuilder中的Textfeild值

时间:2020-09-10 20:46:53

标签: flutter

我是新手,我正在从事一个项目,而且时间有限。

所以,我正面临这个问题,Texfields在调用“ getuserinfo”函数后没有更新其值。

我尝试了serval解决方案,但没有一个对我有用。

我要做的是在从将来的功能中获取所需的数据后构建视图。我想念什么吗? 这是我的代码:

      class _SettingState extends State<Setting> {
    
    
    String name,email,phone;
    
    
    TextEditingController namecontroller=TextEditingController();
    TextEditingController imagecontroller =TextEditingController();
    TextEditingController useridcontroller =TextEditingController();
    TextEditingController countryidcontroller=TextEditingController();
    TextEditingController cityidcontroller =TextEditingController();
    
    Future<User> _user;
    
    @override
      void initState() {
       super.initState();
       _user = getuserinfo(widget.userid,widget.token);
       setTextfeilds();
      }
    
    setTextfeilds(){
      
      setState(() {
      namecontroller.text = name;
      imagecontroller.text = phone;
      useridcontroller.text = email;
      });
    }
    .
    .
    .
     
        FutureBuilder(
                        future:_user,
                        // initialData: InitialData,
                        builder: (context,AsyncSnapshot<User> snapshot){
                            switch (snapshot.connectionState) {
                            case ConnectionState.active:
                            case ConnectionState.waiting:
                            return Center(child: CircularProgressIndicator());
                            break;
                            case ConnectionState.done:
                            
                              if (snapshot.hasData && !snapshot.hasError) {
                                if (snapshot.data == null) {
                                  return Text("No Data",style: new TextStyle(  fontSize: 20.0));
                                } else {
                                    name = snapshot.data.userdata.name;
                                    phone = snapshot.data.userdata.phone;
                                    email = snapshot.data.userdata.email;
                                    }
                                  }else{
                                   return  Center(child: CircularProgressIndicator());
                                  }
                                  
                              }
                           
                          },
                      ),
.
.
.
   Container(
                    height: size.height * 0.0739,
                    decoration: BoxDecoration(
                        color: NeumorphicTheme.baseColor(context),
                        // border: Border.all(color: Hexcolor('#707070')),
                        borderRadius: BorderRadius.circular(29)),

                    child: Directionality(
                      textDirection: TextDirection.ltr,
                      child: TextField(
                        textAlign: TextAlign.right,
                        controller:namecontroller,
                        onChanged: setTextfeilds(),
                        decoration: InputDecoration(
                          enabledBorder: InputBorder.none,
                          errorBorder: InputBorder.none,
                          disabledBorder: InputBorder.none,
                          focusedBorder:
                              UnderlineInputBorder(borderSide: BorderSide.none),
                          prefixIcon: SizedBox(
                            width: 10,
                          ),
                          suffixIcon: Image.asset('assets/images/man.png'),
                          labelText: 'الاسم',
                          labelStyle: GoogleFonts.sourceSansPro(
                            fontSize: 20.0,
                          ),
                        ),
                      ),
                    ),

                  

                    // ],)
                  ),

1 个答案:

答案 0 :(得分:0)

因为可能无法完成setTextFields,所以将initState方法从future中移出了,而是在setTextFields中调用了FutureBuilder方法。

我以您的代码为例添加了一个演示:


InitState:

@override
void initState() {
  super.initState();
  _user = getuserinfo(widget.userid, widget.token);
}

setTextfeilds({String name, String phone, String email}) { // pass your user data parameters
  namecontroller.text = name;
  imagecontroller.text = phone;
  useridcontroller.text = email;
}

FutureBuilder:

   FutureBuilder(
      future: _user,
      // initialData: InitialData,
      builder: (context, AsyncSnapshot<User> snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.active:
          case ConnectionState.waiting:
            return Center(child: CircularProgressIndicator());
            break;
          case ConnectionState.done:
            if (snapshot.hasData && !snapshot.hasError) {
              if (snapshot.data == null) {
                return Text("No Data", style: new TextStyle(fontSize: 20.0));
              } else {
                // call the setTextFields method when the future is done
                setTextfeilds(
                  name: snapshot.data.userdata.name,
                  phone: snapshot.data.userdata.phone,
                  email: snapshot.data.userdata.email,
                  return .... // your text fields
                );
              }
            } else {
              return Center(child: CircularProgressIndicator());
            }
        }
      },
    ),