gpt4 book ai didi

flutter - 如何使用Flutter将Firebase中的值设置为TextFieldForm

转载 作者:行者123 更新时间:2023-12-03 04:50:06 25 4
gpt4 key购买 nike

我正在使用StreamBuilder从Firestore获取数据。
但是我不知道如何在TextFormField上设置特定的数据。
我正在尝试使用TextEditingController将文本放在TextFormField上。

我可以获取Firestore的文档ID。

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),
),
),
),
],
);
}
}
),
),
);
}
}

你能给我一些建议吗?

最佳答案

您可以使用TextEditingController并根据Firestore更新TextFormFieldinitState的值。

  // 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 - 如何使用Flutter将Firebase中的值设置为TextFieldForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61554341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com