questionController.text 总是返回一个空值。而不是我在 textformfield 中插入的任何内容。
class _AddQuestionState extends State<AddQuestion> {
TextEditingController questionController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: questionController,
decoration: InputDecoration(
hintText: 'description',
hintStyle: TextStyle(
color: Colors.grey
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
),),
AddUser(questionController.text),]
}}
当我调用这个类并打印输出时,它返回 null。
使用 AddUser(questionController.text);
我使用 print(question) 打印了输出,但它返回了空字符串。
class AddUser extends StatelessWidget {
final String question;
AddUser(this.question);
@override
Widget build(BuildContext context) {
// Create a CollectionReference called users that references the firestore collection
CollectionReference users = FirebaseFirestore.instance.collection('Questions').doc("cse").collection("CSE");
Future<void> addUser() {
print(question);
// Call the user's CollectionReference to add a new user
return users
.add({
'question': question, // John Doe
})
.then((value) => print("User Added"))
.catchError((error) => print("Failed to add user: $error"));
}