[英]Adding data to collection firestore flutter with relevant user
This is just some basic registration ui这只是一些基本的注册用户界面
I need to send these data when user is registering.我需要在用户注册时发送这些数据。
Currently I need to send First Name and Last Name.目前我需要发送名字和姓氏。
Sending data is okay but they don't have a field.发送数据没问题,但它们没有字段。
Is there a way to add user id when I click Sign Up button?当我点击注册按钮时,有没有办法添加用户 ID?
So how do I send that data according to relevant user???那么如何根据相关用户发送该数据???
And how to retrieve that data with relevant user?以及如何与相关用户检索该数据?
Hope you help me on this希望你能帮助我
Thank you谢谢
class RegisterView extends StatefulWidget {
const RegisterView({Key? key}) : super(key: key);
@override
State<RegisterView> createState() => _RegisterViewState();
}
class _RegisterViewState extends State<RegisterView> {
late final TextEditingController _email;
late final TextEditingController _password;
late final TextEditingController _firstName;
late final TextEditingController _lastName;
final _formKey = GlobalKey<FormState>();
@override
void initState() {
_email = TextEditingController();
_password = TextEditingController();
_firstName = TextEditingController();
_lastName = TextEditingController();
super.initState();
}
@override
void dispose() {
_firstName.dispose();
_lastName.dispose();
_email.dispose();
_password.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color.fromARGB(255, 185, 185, 185),
appBar: AppBar(
backgroundColor: const Color.fromARGB(255, 87, 127, 160),
elevation: 0,
leading: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: const Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
),
),
body: ListView(
children: [
Container(
height: 150,
padding: const EdgeInsets.only(top: 40),
decoration: const BoxDecoration(
color: Color.fromARGB(255, 87, 127, 160),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(150),
bottomRight: Radius.circular(150),
),
),
child: const Text(
'Create New Account',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 35,
color: Colors.black,
),
),
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 40,
vertical: 30,
),
child: Form(
key: _formKey,
child: Column(
children: [
// First name and last name
Row(
children: [
Flexible(
// First Name
child: TextFormField(
decoration: const InputDecoration(
labelText: 'First Name',
),
validator: (val) => val!.isEmpty ? 'requied*' : null,
controller: _firstName,
),
),
const SizedBox(
width: 20.0,
),
Flexible(
// Last Name
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Last Name',
),
validator: (val) => val!.isEmpty ? 'requied*' : null,
controller: _lastName,
),
),
],
),
// Email Address
TextFormField(
decoration: const InputDecoration(
labelText: 'Email address',
),
keyboardType: TextInputType.emailAddress,
controller: _email,
validator: (val) => val!.isEmpty ? 'requied*' : null,
),
// Password
TextFormField(
decoration: const InputDecoration(
labelText: 'Create a Password',
),
controller: _password,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
validator: (val) => (val!.isEmpty && val.length < 5)
? 'Enter a password with more than 6 characters'
: null,
),
const SizedBox(
height: 20,
),
// Sign Up Button
TextButton(
style: TextButton.styleFrom(
backgroundColor: const Color.fromARGB(255, 0, 132, 255),
),
onPressed: () async {
final email = _email.text;
final password = _password.text;
if (_formKey.currentState!.validate()) {
try {
final userCredential =
await AuthService.firebase().register(
email: email,
password: password,
);
Map<String, dynamic> data = {
'first_name': _firstName.text,
'last_name': _lastName.text,
};
await FirebaseFirestore.instance
.collection('user')
.add(data);
devtools.log(userCredential.toString());
await showErrorDialog(
context,
'Registration Successful !\nYou can log in now.',
).then(
(value) {
Navigator.of(context).pushNamedAndRemoveUntil(
loginRoute, (route) => false);
},
);
} on InvalidEmailAuthException {
await showErrorDialog(
context,
'Invalid email',
);
} on WeakPasswordAuthException {
await showErrorDialog(
context,
'Weak password',
);
} on EmailAlreadyUsedAuthException {
await showErrorDialog(
context,
'Email already in use',
);
} on GenericAuthException {
await showErrorDialog(
context,
'Failed to register',
);
}
}
},
child: const Text(
'Sign Up',
style: TextStyle(
color: Colors.white,
),
),
),
],
),
),
),
],
),
);
}
}
Your current code for writing the user data is:您当前用于编写用户数据的代码是:
FirebaseFirestore.instance
.collection('user')
.add(data);
This generates a unique ID for the data.这会为数据生成一个唯一 ID。
If you want to store the data under the UID of the user, you can do so with:如果要将数据存储在用户的 UID 下,可以使用:
FirebaseFirestore.instance
.collection('user')
.doc(userCredential.user!.uid)
.set(data);
Also see:另见:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.