How to fetch the data from cloud_firestore and display it in the TextField in flutter

Padding(
padding: const EdgeInsets.all(14.0),
 child: Container(
   child: TextFormField(
     initialValue: fullName,
       cursorColor: Colors.deepPurpleAccent,
         decoration: InputDecoration(
          hintText: 'Full Name',
            focusedBorder: InputBorder.none,
             focusColor: Colors.deepPurpleAccent,
             enabledBorder: OutlineInputBorder(
             borderSide: BorderSide(
              color: Colors.deepPurpleAccent,
                 ),
                ),
              ),
            ),
          ),
        ),



String fullName;
void _fetchUserData() async {
try {
  FirebaseUser _currentUser = await FirebaseAuth.instance.currentUser();
  String authid =_currentUser.uid;
  Firestore.instance.collection('UserData').document('$authid').get().then((ds) {
    if(ds.exists)
    {
      setState(() {
       fullName = ds.data['FullName'];
      });
    }

  });

} catch (e) {
  print(e);
}

}

@override
void initState() {
  super.initState();
  _fetchUserData();
}

” In this TextFormField I am displaying that value but it is displaying null

” I am trying to do this in this manner but it is showing the null value in the Android Emaluater TextFormField

Answer

TextFormField’s initial value is only used to initialize its TextController. The TextFormField’s value is maintained by the TextEditingController.

  final controller = TextEditingController();

  void _fetchUserData() async {
    // do something
    setState(() {
      controller.text = "your text";
    });
  }

  Widget build(BuildContext context) {
    return TextFormField(
      controller: controller,
    );
  }

  void dispose() {
    myController.dispose();
    super.dispose();
  }

Leave a Reply

Your email address will not be published. Required fields are marked *