如何使TextFormField中写入的数据更新已记录的数据
这是我的登录屏幕。这是注册页面的一部分。登录页面上的相同代码
String _email, _password, id;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final db = Firestore.instance;
Expanded(
child: TextFormField(
autofocus: false,
// controller: _email,
// validator: Validator.validateEmail,
onSaved: (input) => _email = input,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter your email',
hintStyle: TextStyle(color: Colors.grey),
),
),
),
Expanded(
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: Colors.blue,
onPressed: () {},
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 20.0),
child: Text(
'SIGN UP',
style: TextStyle(color: Colors.white),
),
),
Expanded(
child: Container(),
),
Transform.translate(
offset: Offset(10.0, 0.0),
child: Container(
padding: EdgeInsets.all(1.0),
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(28.0)
),
color: Colors.white,
child: Icon(
Icons.arrow_forward,
color: Colors.blue,
),
onPressed: () async {
// _emailSignUp(
// email:_email.text,
// password:_password.text,
// context: context
// );
createData();
signUp();
sharedPreferences = await
SharedPreferences.getInstance();
sharedPreferences.setString("email", _email);
},
),
),
)
],
),
),
)
void createData() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
DocumentReference ref = await db.collection('users').add({'email': '$_email', 'name': 'UserName', 'userPhotoURL': 'url'});
setState(() => id = ref.documentID);
print(ref.documentID);
}
}
AccountScreen。在这里,我想通过TextFormField更新数据用户。我想根据进来的用户确定需要更改哪些数据
class AccountScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => AccountScreenState();
}
class AccountScreenState extends State<AccountScreen> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String countries;
String selected;
SharedPreferences sharedPreferences;
String _name, _email;
final db = Firestore.instance;
@override
void initState() {
super.initState();
getDataPreference();
}
getDataPreference() async {
sharedPreferences = await SharedPreferences.getInstance();
setState(() {
_email = sharedPreferences.getString("email");
});
}
// void updateData(DocumentSnapshot doc) async {
// await db.collection('users').document(doc.documentID).updateData({'email': '$_email', 'name': '$_name'});
// Navigator.of(context).pushReplacementNamed('/home_screen');
// }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Your account"), backgroundColor: Colors.blueGrey[900]),
body:
Container(
color: Colors.blueGrey[800],
child: Form(
key:_formKey,
child: ListView(
children: <Widget>[
AccountImage(),
ListTile(
leading: Icon(Icons.account_box, color: Colors.white),
title: TextFormField(
onSaved: (input) => _name = input,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
fillColor: Colors.white,
hintText: "Name",
hintStyle: TextStyle(color: Colors.white)),
)),
ListTile(
leading: Icon(Icons.email, color: Colors.white),
title: TextFormField(
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
fillColor: Colors.white,
hintText: "Email",
hintStyle: TextStyle(color: Colors.white)),
// keyboardType: TextInputType.emailAddress,
initialValue: _email,
onSaved: (input) => _email = input,
),
),
ListTile(
leading: Icon(Icons.language, color: Colors.white),
title: DropDownCountries(),
),
Container(
padding: EdgeInsets.all(15.0),
child: Material(
color: Colors.blue[700],
elevation: 3.0,
child: MaterialButton(
height: 45.0,
child: Text("Save", style: TextStyle(color: Colors.white)),
onPressed: () {
// updateData();
Navigator.of(context).pushReplacementNamed('/home_screen');
}
),
),
)
],
),
)
),
);
}
}
你可以update
这样的记录你可以从文件中获得documentID
Firestore.instance
.collection('users')
.document(userID)// use documentID here to update particular document
.updateData({
"email": emailController.text,//This is the data i.e going to be updated
"City": cityController.text,//This is the data i.e going to be updated
"Phone": phoneNumberController.text.toString(),//This is the data i.e going to be updated
});