Home > Net >  set firebase data in textfield flutter dart
set firebase data in textfield flutter dart

Time:11-25

What I want to do : When the user want to change their name on app, User going to modify : when the user showing this screen, already enter his name in this textfiled. He can change it.

Example: In the picture, the user going to modify his username His firebase username is "default". It's already typed. he remove "t" and submit.

enter image description here

My code here

 class ModifyScreen extends StatefulWidget {
      final String uid;
      const ModifyScreen({
        super.key,
        required this.uid,
      });
    
      @override
      State<ModifyScreen> createState() => _ModifyScreenState();
    }
    
    class _ModifyScreenState extends State<ModifyScreen> {
      var userData = {};
      bool isLoading = false;
    
      @override
      void initState() {
        getData();
        super.initState();
      }
    
      getData() async {
        
        try {
          var userSnap = await FirebaseFirestore.instance
              .collection('users')
              .doc(widget.uid)
              .get();
    
          userData = userSnap.data()!;
          setState(() {});
        } catch (e) {
          showSnackBar(
            context,
            e.toString(),
          );
        }
        
      }
    
      @override
      Widget build(BuildContext context) {
        TextEditingController _namecon = TextEditingController();
        return Scaffold(
                body: Padding(
                  padding: const EdgeInsets.all(18),
                  child: Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        TextField(
                          controller: _namecon,
                          decoration:
                              InputDecoration(hintText: userData['username']),
                        )
                      ],
                    ),
                  ),
                ),
              );
      }
    }

CodePudding user response:

Remove decoration and add _namecon.text = userData['username'] in build, also take TextEditingController _namecon = TextEditingController() outside of build

  • Related