首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过云FireStore在TextFormField中提供initialValue

如何通过云FireStore在TextFormField中提供initialValue
EN

Stack Overflow用户
提问于 2020-05-21 03:33:27
回答 3查看 469关注 0票数 3

我正在尝试通过表单更新云Firestore中的数据。我需要已经存储在firestore中的字段在TextFormField中显示为initialValue。

获取的数据通过print()在控制台上打印,但在TextFormField中不显示任何内容。

initialValue与我用于print()的代码行相同。

数据也会被正确获取。

代码如下:

代码语言:javascript
运行
复制
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;
                  },
                ),
              ),
            ),
           }
EN

Stack Overflow用户

发布于 2020-05-21 04:01:04

代码语言:javascript
运行
复制
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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61921429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档