如何在文本字段颤动中获得价值

how to get value in textfield flutter

如何在文本字段中输入值? 我有从 api 获得的值,即名称数据 我在这里编辑数据,然后想更新数据的内容。 但如果它不编辑数据,我仍然对初始化数据感到困惑

我附上代码:

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(),
    );
  }
}

尝试下面的答案我添加了一行以在文本字段上获取值

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(),
    );
  }
}