Search code examples
flutterflutter-layoutflutter-android

how to get value in textfield flutter


how to enter value into textfield ? i have the value i get from api, that is name data here i edit the data and then want to update the contents of the data. but I'm still confused about initializing the data if it doesn't edit the data

I attach the code:

class ProfileData extends StatefulWidget {
  @override
  _ProfileDataState createState() => _ProfileDataState();
}

class _ProfileDataState extends State<ProfileData> {

  TextEditingController fnameController = TextEditingController(text: '');
  
bool isLoading = false;

  @override
  Widget build(BuildContext context) {

    UserProvider user = Provider.of<UserProvider>(context);
    //here i call api using provider, if I print it will produce a value which is a name 'alu card' , ex : user.user.fname -> value('alu card')

    handleSubmit() async {
      setState(() {
        isLoading = true;
      });

      if (await user.submit( //method put to service
        fname: fnameController.text,
      )) {
        Navigator.pushNamed(context, '/main');
      } else { 
       print('err');
      }

      setState(() {
        isLoading = false;
      });
    }

   Widget input() {
      return Container(
        child: Column(
          children: [
            Expanded(
                child: TextFormField(
              style: blackTextStyle,
              controller: fnameController,
              decoration: InputDecoration.collapsed(
                hintText: user.user.fname, // this value 'alucard'
                hintStyle: blackTextStyle,
              ),
            ))
          ],
        ),
      );
     ....... //widget button submit 
     .......
      onpress : handleSubmit
    }

    return Scaffold(
      backgroundColor: Colors.white,
      body: input(),
    );
  }
}

Solution

  • Try out below answer I added line for getting value on Textfield

    class _ProfileDataState extends State<ProfileData> {
    
      TextEditingController fnameController = TextEditingController(text: '');
      
    bool isLoading = false;
    
      @override
      Widget build(BuildContext context) {
    
        UserProvider user = Provider.of<UserProvider>(context);
        fnameController.text = user.user.fname  // Added this line here.
    
        handleSubmit() async {
          setState(() {
            isLoading = true;
          });
    
          if (await user.submit( //method put to service
            fname: fnameController.text,
          )) {
            Navigator.pushNamed(context, '/main');
          } else { 
           print('err');
          }
    
          setState(() {
            isLoading = false;
          });
        }
    
       Widget input() {
          return Container(
            child: Column(
              children: [
                Expanded(
                    child: TextFormField(
                  style: blackTextStyle,
                  controller: fnameController,
                  decoration: InputDecoration.collapsed(
                    hintText: user.user.fname, // this value 'alucard'
                    hintStyle: blackTextStyle,
                  ),
                ))
              ],
            ),
          );
         ....... //widget button submit 
         .......
          onpress : handleSubmit
        }
    
        return Scaffold(
          backgroundColor: Colors.white,
          body: input(),
        );
      }
    }