简体   繁体   English

flutter中Firestore数据变化结果为null

[英]Firestore data change results is null in flutter

Collection name is userInfo & the data I want to update is name.集合名称是 userInfo & 我要更新的数据是名称。 But when I do that using textfield onChanged value, then the update method changes the value in the collection document to null value.但是当我使用文本字段 onChanged 值执行此操作时,更新方法会将集合文档中的值更改为 null 值。

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:hindusofindia/model/user.dart';
import 'package:hindusofindia/screens/user/UserProfile.dart';

class EditName extends StatefulWidget {
  EditName({this.email});

  final String email;

  @override
  _EditNameState createState() => _EditNameState();
}

class _EditNameState extends State<EditName> {
  void _changeName(String value) async {
    Firestore.instance
        .collection("userInfo")
        .document(widget.email)
        .updateData({"name": value});
  }

  @override
  Widget build(BuildContext context) {
    String nameChange='6767';

    return Scaffold(
      appBar: AppBar(
        title: Text('Edit Name'),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: <Color>[
                Colors.orange,
                Colors.red,
              ],
            ),
          ),
        ),
      ),
      body: Column(
        children: <Widget>[
          TextField(
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'Edit Name',
            ),
            onChanged: (text) {
              setState(() {
                nameChange = text;
              });
            },
          ),
          FlatButton(
            onPressed: () {
              if (nameChange != '6767') {
                _changeName(nameChange);
              }
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => UserScreen(
                    currentUser: User(email: widget.email),
                  ),
                ),
              );
            },
            child: Text('Apply changes'),
          )
        ],
      ),
    );
  }
}

But what is supposed to happen is that the textfield name changes results in updating the name value in firebase document collection to the textfield value.但应该发生的是文本字段名称更改导致将 firebase 文档集合中的名称值更新为文本字段值。 Changes will occur after flatbutton is pressed later.稍后按下 flatbutton 后会发生变化。

onChanged: (text) {
  setState(() {
    nameChange = text;
});

setState() will call the build() method again to rebuild the widget tree, but since you declared nameChange inside the build() method it will have the same initial value when clicking FlatButton . setState()将再次调用build()方法来重建小部件树,但是由于您在build()方法中声明了nameChange ,因此在单击FlatButton时它将具有相同的初始值。 You should declare as an instance variable under the class:你应该在class下声明为一个实例变量:

class _EditNameState extends State<EditName> {
String nameChange = "6767";
//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 firestore 中有数据时检索到 null flutter - Retrieved null when there is data in firestore with flutter 试图在 firestore 子集合中获取文档数据,然后在 flutter 上显示此错误“null 值上使用的空检查运算符”? - Tried to fetch document data in firestore subcollection then show this error "Null check operator used on a null value "on flutter? Flutter Firestore 添加数据 - Flutter Firestore adding data Flutter 错误 - 断言失败:第 213 行 pos 15:'data:= null':在从 firestore 获取数据时不正确 - Flutter error - failed assertion: line 213 pos 15: 'data != null': is not true at the time of fetching data from firestore Flutter Firestore:将数据列表添加到 Firestore 文档 - Flutter Firestore : Add a List of Data to Firestore Document StreamBuilder 未更新 Flutter 中的 Firestore 数据 - StreamBuilder not updating Firestore data in Flutter 如何在 flutter 中对 firestore 数据进行分页 - how to paginate firestore data in flutter Flutter 当firestore中有数据时,firestore返回长度0 - Flutter firestore returns length 0 while there is data in firestore 如何使 Firestore 搜索结果显示在 flutter 中? - How to make firestore search results display in flutter? 在 firestore 中自动更新 flutter 查询结果 - Auto update flutter query results in firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM