本文介绍了如何使用Flutter将Firebase中的值设置为TextFieldForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用StreamBuilder从Firestore获取数据。
但是我不知道如何在TextFormField上设置特定数据。
我正在尝试使用TextEditingController将文本放在TextFormField上。
I'm using StreamBuilder to get data from Firestore.However I have no idea how to set specific data on TextFormField.I'm trying to use TextEditingController to put text on TextFormField.
我可以获取Firestore的文档ID。
I can get document ID for firestore.
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class EditPage extends StatefulWidget {
final String memberID;
EditPage({Key key, @required this.memberID});
@override
_EditPageState createState() => _EditPageState();
}
class _EditPageState extends State<EditPage> {
TextEditingController lastNameController = TextEditingController();
TextEditingController firstNameController = TextEditingController();
void _editMember() {
Firestore.instance.collection('members').document('cGbVJaL7Ib4S3cKXBQmE').updateData({
'last_name': lastNameController.text,
});
setState(() {
});
}
@override
void initState(){
super.initState();
}
@override
Widget build(BuildContext context) {
final _formKey = GlobalKey<FormState>();
return Scaffold(
appBar: AppBar(
title: Text('Update Member'),
),
body: Form(
key: _formKey,
child: new StreamBuilder <QuerySnapshot>(
stream: Firestore.instance.collection('members').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if(snapshot.hasError){
return new Text('Error: ${snapshot.error}');
}
switch (snapshot.connectionState){
case ConnectionState.waiting:
return new Text('Loading...');
default:
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
Text('${widget.memberID}'),
TextFormField(
controller: lastNameController,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 10),
border: OutlineInputBorder(),
filled: true,
icon: Icon(Icons.account_circle),
labelText: 'Name',
),
validator: (value){
if(value.isEmpty){
return 'Requierd';
}
return null;
},
),
new GestureDetector(
onTap: (){
print('Register!!');
_editMember();
Navigator.pop(context);
},
child:
new Container(
height: 54.00,
width: 311.00,
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Update'),
]
),
decoration: BoxDecoration(
color: Color(0xffffa031),borderRadius: BorderRadius.circular(27.00),
),
),
),
],
);
}
}
),
),
);
}
}
您能给我一些建议吗?
Would you give me some advice?
推荐答案
您可以使用 TextEditingController
并更新<$ c基于Firestore的 initState
中的$ c> TextFormField 。
You could use TextEditingController
and update the value of TextFormField
in initState
based on Firestore.
// Declare your controller and set the initial value
TextEditingController firstNameController = TextEditingController(text: 'initial value');
@override
initState() {
Firestore.instance
.collection('members')
.snapshots()
.listen((QuerySnapshot snapshot) {
// Update the TextFormField value here
firstNameController.text = snapshot.documents.first['something'];
});
super.initState();
}
这篇关于如何使用Flutter将Firebase中的值设置为TextFieldForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!