我对 firebase 真的很陌生,我正在尝试从云 Firestore 中获取数据并将其设置为 flutter 中的 textformfield,但我的努力没有取得成效。我想从 firestore 数据库中获取一个批次号,并将其设置为我的应用程序中的 _batchTextEditingController。这是查看我的 Cloud Firestore 数据库的链接 [Click here to view firestore database ] 和下面是我的代码。
class _RegisterState extends State<Register> {
final TextEditingController _nameTextEditingController =
TextEditingController();
final TextEditingController _emailTextEditingController =
TextEditingController();
final TextEditingController _passwordTextEditingController =
TextEditingController();
final TextEditingController _cPasswordTextEditingController =
TextEditingController();
//This is the textfield i want to set ... please help
TextEditingController _batchTextEditingController =
TextEditingController();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String userImageUrl = "";
File _imageFile;
@override
Widget build(BuildContext context) {
double _screenWidth = MediaQuery.of(context).size.width;
double _screenheight = MediaQuery.of(context).size.height;
return SingleChildScrollView(
child: Container(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
SizedBox(height: 10.0),
InkWell(
onTap: () => _selectAndPickImage(),
child: CircleAvatar(
radius: _screenWidth * 0.15,
backgroundColor: Colors.white,
backgroundImage:
_imageFile == null ? null : FileImage(_imageFile),
child: _imageFile == null
? Icon(
Icons.add_a_photo_sharp,
size: _screenWidth * 0.15,
color: Colors.grey,
)
: null,
),
),
SizedBox(height: 8.0),
Form(
key: _formKey,
child: Column(
children: [
CustomTextField(
controller: _nameTextEditingController,
data: Icons.person,
hintText: "Name",
isObsecure: false,
),
CustomTextField(
controller: _emailTextEditingController,
data: Icons.email,
hintText: "Email",
isObsecure: false,
),
CustomTextField(
controller: _passwordTextEditingController,
data: Icons.person,
hintText: "Password",
isObsecure: true,
),
CustomTextField(
controller: _cPasswordTextEditingController,
data: Icons.person,
hintText: "Confirm password",
isObsecure: true,
),
CustomTextField(
controller: _batchTextEditingController,
data: Icons.book,
hintText: "Batch",
isObsecure: false,
),
],
),
),
RaisedButton(
onPressed: () {
uploadAndsaveImage();
},
color: Colors.pink,
child: Text(
"Signup",
style: TextStyle(color: Colors.white),
),
),
SizedBox(height: 30),
Container(
height: 4.0,
width: _screenWidth * 0.8,
color: Colors.pink,
),
SizedBox(height: 15),
],
),
),
);
}
答案 0 :(得分:0)
使用 FutureBuilder
!
FutureBuilder<DocumentSnapshot>(
future: FirebaseFirestore.instance.collection('data').doc('docID').get(),//Get the data from cloud firestore
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return TextFormField(
initialValue: snapshot.data,//Inserts into the form as initial value
);
}
return Center(
child: CircularProgressIndicator(),
);
},
);