我正在尝试通过表单更新云Firestore中的数据。我需要已经存储在firestore中的字段在TextFormField中显示为initialValue。
获取的数据通过print()在控制台上打印,但在TextFormField中不显示任何内容。
initialValue与我用于print()的代码行相同。
数据也会被正确获取。
代码如下:
var initData = {
'title': '',
 };
void didChangeDependencies() async{
super.didChangeDependencies();
await Firestore.instance
    .collection('${widget.collection}')
    .document('${widget.title}')
    .get()
    .then((value) {
  setState(() {
    initData = {
      'title': value.data['title'],
    };
  });
});
if (isValid) {
  _formKey.currentState
      .save();
  }
  widget.submitFn(
    _title,)
 }
@override
Widget build(BuildContext context) {
print(initData['title']); // the fetched data is printed in the console
return Form(
  key: _formKey,
 Column(
   children: <Widget>[
               TextFormField(
                  initialValue: initData['title'], // this doesnt show anything on the TextFormField,
                  decoration: InputDecoration(
                      labelText: 'Enter Product Name',
                      contentPadding: EdgeInsets.all(5)),
                  validator: (value) {
                    if (value.isEmpty) {
                      return 'Enter a the Product Title';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    _title = value;
                  },
                ),
              ),
            ),
           }发布于 2020-05-21 04:01:04
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  TextEditingController controller;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller = TextEditingController();
    controller.text = "hiii";  //Here you can provide a default value when your app starts.
    controller.addListener(() {
      print(controller.text);
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: Form(
              child: TextField(
                controller: controller,
              ),
            ),
          ),
        ),
      ),
    );
  }
}https://stackoverflow.com/questions/61921429
复制相似问题