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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to fetch the data from cloud_firestore and display it in the TextField in flutter without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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();
  }
We are here to answer your question about How to fetch the data from cloud_firestore and display it in the TextField in flutter - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji