Flutter Sending data to Firebase.(problem with cupertinopicker)

137 views Asked by At

First Sorry about my bad English and I just started to learn Flutter. So I want to get all the informations in Firestore and I cant solve these problems.

Question 1:

If i click the select button, Cupertinopicker will show up and the result will show right next to the button. So If I pick b, i want the result sended to the Firestore. and I have no idea how i can...with the CupertinoPicker... I would also like to know how i can use the validator and show the error sign too enter image description here

This is the code below with the Cupertinopicker. I want the Text(_countryType[_selectedIndex] sendend to Firebase.

 Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              CupertinoButton(
                borderRadius: BorderRadius.circular(29.0),
                color: kPrimaryColor,
                padding: const EdgeInsets.all(12.0),
                child: Text(
                  "select",
                  style: TextStyle(fontSize: 16.0),
                ),
                onPressed: () {
                  showModalBottomSheet(
                      context: context,
                      builder: (BuildContext context) {
                        return Container(
                          height: 170.0,
                          child: CupertinoPicker(
                              scrollController:
                                  new FixedExtentScrollController(
                                initialItem: _selectedIndex,
                              ),
                              itemExtent: 32.0,
                              onSelectedItemChanged: (int index) {
                                setState(() {
                                  _country = _countryType[index];
                                  _selectedIndex = index;
                                });
                              },
                              children: new List<Widget>.generate(
                                  _countryType.length, (int index) {
                                return new Center(
                                  child: new Text(_countryType[index]),
                                );
                              })),
                        );
                      });
                },
              ),
              Container(
                margin: EdgeInsets.symmetric(vertical: 17),
                width: 70,
                child: Center(
                  child: Text(
                    _countryType[_selectedIndex],
                    style: TextStyle(fontSize: 16.0),
                  ),
                ),
              ),
              SizedBox(
                height: 20.0,
              ),
            ],
          ),

Question2: I want all email, password, name, alphabet(the one with the cupertinopicker) sended to the firestore User. So i want to put it in [User- uid- fields ]I'm also stucked here too.

This is the Signup button below.

Container(
            margin: EdgeInsets.symmetric(vertical: 10),
            width: size.width * 0.8,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(29),
              child: FlatButton(
                padding: EdgeInsets.symmetric(vertical: 20, horizontal: 40),
                color: kPrimaryColor,
                onPressed: () async {
                  try {
                    FirebaseUser user = (await FirebaseAuth.instance
                            .createUserWithEmailAndPassword(
                      email: _emailController.text,
                      password: _passwordController.text,
                    ))
                        .user;
                    if (user != null) {
                      UserUpdateInfo updateUser = UserUpdateInfo();
                      updateUser.displayName = _usernameController.text;
                      user.updateProfile(updateUser);
                      Navigator.of(context).pushNamed(AppRoutes.authLogin);
                    }
                  } catch (e) {
                    print(e);
                    _usernameController.text = "";
                    _passwordController.text = "";
                    _repasswordController.text = "";
                    _emailController.text = "";
                    
                  }
                  setState(() {
                    saveAttempted = true;
                  });
                  if (_formKey.currentState.validate()) {
                    _formKey.currentState.save();
                  }
                },
                
                child: Text(
                  "Sign Up",
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),

Which code do I need to use.... It would be super helpful if someone help me..Im so stressed out. Thank you very much

1

There are 1 answers

0
Pulin Shah On

I am assuming that you are aware of the basics of how to use Firebase with Flutter.
For the first question, all you need to do is call a function inside

                              onSelectedItemChanged: (int index) {
                            setState(() {
                              _country = _countryType[index];
                              _selectedIndex = index;
                            });
                          },

What happens here is, whenever you select an item. onSelectedItemChanged is called. So all you need to do is call a function here
Example -

                              onSelectedItemChanged: (int index) {
                            addToFirebase(_countryType[_selectedIndex]);
                            setState(() {
                              _country = _countryType[index];
                              _selectedIndex = index;
                            });
                          },

For your second question, Firebase authentication doesn't work like that. User details are stored in the Authentication area of Firebase. You cannot see the password as well. To store the country type attached with the user, you can use the User's Id as the key as it will be unique.

                FirebaseUser user = (await FirebaseAuth.instance
                        .createUserWithEmailAndPassword(
                  email: _emailController.text,
                  password: _passwordController.text,
                ))
                    .user;
                String uid = user.uid;