DateTime 在发送到 Firestore 时变为 null。

huangapple go评论50阅读模式
英文:

DateTime becomes null when I send to Firestore

问题

我已经创建了一个TextFormField来将DateTime发送到Firestore集合,但当我发送信息时,“datasaida”字段为空:

这是代码:

class AddSaida extends StatefulWidget {
  AddSaida({Key? key}) : super(key: key);

  @override
  State<AddSaida> createState() => _AddSaidaState();
}

class _AddSaidaState extends State<AddSaida> {
  
  String? valorsaida, nomesaida, datasaida;

  getSaidaValue(valorsaida){
    this.valorsaida = valorsaida;
  }
  getSaidaName(nomesaida){
    this.nomesaida = nomesaida;
  }
  getSaidaDate(datasaida){
    this.datasaida = datasaida;
  }


  createData() {
    DocumentReference ds = FirebaseFirestore.instance.collection('addsaidas').doc(nomesaida);
    Map<String, dynamic> tasks = {
      "valorsaida": valorsaida,
      "nomesaida": nomesaida,
      "datasaida": datasaida,
      "tipocategoria": categVal,
    };

    ds.set(tasks).whenComplete(() {
      print("task updated");
    });
  }

  var saidanamecontroller = TextEditingController();
  var saidadatecontroller = TextEditingController();
  var saidavaluecontroller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromARGB(255, 92, 172, 178),
        centerTitle: true,
        title: Text('Adicionar saída'),
        toolbarHeight: 90,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(40),
        ),        
        elevation: 15,
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: const EdgeInsets.all(28),
          child: Column(
            children: [
              TextFormField(
                controller: saidanamecontroller,
                onChanged: (String nomesaida) {
                  getSaidaName(nomesaida);
                },
                autofocus: true,
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
                  hintText: "Nome da saída",
                  hintStyle: TextStyle(
                    color: Color.fromARGB(255, 138, 136, 136),
                    fontSize: 18,
                  ),
                  prefixIcon: Icon(
                    Icons.label_important_outline,
                    color: Color.fromARGB(255, 92, 172, 178),
                    size: 30,
                  ),
                  labelText: "Nome da saída",
                  labelStyle: TextStyle(
                    color: Color.fromARGB(255, 136, 136, 136),
                    fontFamily: 'PathwayGothicOne',
                    fontSize: 13,
                  ),
                ),
              ),
              SizedBox(height: 20),
              TextFormField(
                controller: saidadatecontroller,
                onChanged: (datasaida) {
                  getSaidaDate(datasaida);
                },
                autofocus: true,
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
                  hintStyle: TextStyle(
                    color: Color.fromARGB(255, 138, 136, 136),
                    fontSize: 18,
                  ),
                  prefixIcon: Icon(
                    Icons.calendar_month_outlined,
                    color: Color.fromARGB(255, 92, 172, 178),
                    size: 30,
                  ),
                  labelText: "Data da saída",
                  labelStyle: TextStyle(
                    color: Color.fromARGB(255, 136, 136, 136),
                    fontFamily: 'PathwayGothicOne',
                    fontSize: 13,
                  ),
                  enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: Color.fromARGB(153, 191, 190, 190),
                    ),
                  ),
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: Color.fromARGB(153, 191, 190, 190),
                    ),
                  ),
                ),
                onTap: () async {
                  DateTime? pickeddate = await showDatePicker(
                    context: context, 
                    initialDate: DateTime.now(), 
                    firstDate: DateTime(2022), 
                    lastDate: DateTime(2036));

                  if (pickeddate != null) {
                    saidadatecontroller.text = DateFormat('dd-MM-yyyy').format(pickeddate);
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

其他字段正常工作,只有这个字段获取到空值。我尝试将值转换为字符串,但没有成功。

我不知道如何修复这个问题,请帮忙!

英文:

I have created a TextFormField to send DateTime to a firestore collection, but when I send the information it gets a null field on "datasaida":

DateTime 在发送到 Firestore 时变为 null。

This is the code:

class AddSaida extends StatefulWidget {
AddSaida({Key? key}) : super(key: key);
@override
State&lt;AddSaida&gt; createState() =&gt; _AddSaidaState();
}
class _AddSaidaState extends State&lt;AddSaida&gt; {
String? valorsaida,nomesaida,datasaida;
getSaidaValue(valorsaida){
this.valorsaida=valorsaida;
}
getSaidaName(nomesaida){
this.nomesaida=nomesaida;
}
getSaidaDate(datasaida){
this.datasaida=datasaida;
}
createData(){
DocumentReference ds=FirebaseFirestore.instance.collection(&#39;addsaidas&#39;).doc(nomesaida);
Map&lt;String,dynamic&gt; tasks={
&quot;valorsaida&quot;:valorsaida,
&quot;nomesaida&quot;:nomesaida,
&quot;datasaida&quot;:datasaida,
&quot;tipocategoria&quot;:categVal,
};
ds.set(tasks).whenComplete((){
print(&quot;task updated&quot;);
});
}
var saidanamecontroller = TextEditingController();
var saidadatecontroller = TextEditingController();
var saidavaluecontroller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 92, 172, 178),
centerTitle: true,
title: Text(&#39;Adicionar sa&#237;da&#39;),
toolbarHeight: 90,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40)
),        
elevation: 15,
),
body:
SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(28),
child: Column(
children: [
TextFormField(
controller: saidanamecontroller,
onChanged: (String nomesaida){
getSaidaName(nomesaida);
},
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
hintText: &quot;Nome da sa&#237;da&quot;,
hintStyle: TextStyle(color: Color.fromARGB(255, 138, 136, 136),
fontSize: 18),
prefixIcon: Icon(
Icons.label_important_outline,
color: Color.fromARGB(255, 92, 172, 178),
size: 30,
),
labelText: &quot;Nome da sa&#237;da&quot;,
labelStyle: TextStyle(color: Color.fromARGB(255, 136, 136, 136), fontFamily: &#39;PathwayGothicOne&#39;, fontSize: 13),
),
SizedBox(height: 20),
TextFormField(
controller: saidadatecontroller,
onChanged: (datasaida){
getSaidaName(datasaida);
},
autofocus: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(12, 0, 12, 0),
hintStyle: TextStyle(color: Color.fromARGB(255, 138, 136, 136),
fontSize: 18),
prefixIcon: Icon(
Icons.calendar_month_outlined,
color: Color.fromARGB(255, 92, 172, 178),
size: 30,
),
labelText: &quot;Data da sa&#237;da&quot;,
labelStyle: TextStyle(color: Color.fromARGB(255, 136, 136, 136), fontFamily: &#39;PathwayGothicOne&#39;, fontSize: 13),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromARGB(153, 191, 190, 190),
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromARGB(153, 191, 190, 190),
),
),
),
onTap: () async {
DateTime? pickeddate = await showDatePicker(
context: context, 
initialDate: DateTime.now(), 
firstDate: DateTime(2022), 
lastDate: DateTime(2036));
if (pickeddate != null) {
saidadatecontroller.text = DateFormat(&#39;dd-MM-yyyy&#39;).format(pickeddate);
}
},

The other fields are working correctly, this is the only one getting a null value. I have tried to transform the value to a String, but i couldn't.

I don't know how to fix this, please help!

答案1

得分: 1

您之所以出现这种行为,是因为在将其设置到Firestore文档之前,datasaida 在为空时。

您只在onChanged事件上设置datasaida,但还需要在onTap事件上设置它,以便在所有Datepicker事件中它都不会为空,即使在空值时它将具有空字符串""。

在这里,我已经测试了类型为DateTime的单个TextFormField:

class AddSaida extends StatefulWidget {
  const AddSaida({Key? key}) : super(key: key);
  @override
  State<AddSaida> createState() => _AddSaidaState();
}

class _AddSaidaState extends State<AddSaida> {
  final _dateController = TextEditingController();
  String? datasaida;
  getSaidaName(datasaida) {
    this.datasaida = datasaida;
  }

  createData() {
    if (datasaida != null) {
      DocumentReference ds = FirebaseFirestore.instance
          .collection('addsaidas')
          .doc("cnFxQ8wRivY39EywI3TS"); //仅存在的文档ID
      Map<String, dynamic> tasks = {
        "datasaida": datasaida,
      };

      ds.set(tasks).whenComplete(() {
        print("任务已更新");
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          TextFormField(
            controller: _dateController,
            decoration: const InputDecoration(
              labelText: '日期',
            ),
            onTap: () async {
              DateTime? pickedDate = await showDatePicker(
                context: context,
                initialDate: DateTime.now(),
                firstDate: DateTime(2022),
                lastDate: DateTime(2036),
              );
              if (pickedDate != null) {
                _dateController.text =
                    DateFormat('dd-MM-yyyy').format(pickedDate);
                getSaidaName(DateFormat('dd-MM-yyyy').format(pickedDate)); // 注意
              }
            },
            onChanged: (value) {
              getSaidaName(value);
            },
          ),
          ElevatedButton(
            onPressed: createData,
            child: const Text('保存日期'),
          ),
        ],
      ),
    );
  }
}

希望这对您有所帮助!

英文:

You are getting this behavior because datasaida is null before setting it into the firestore document.

You are setting datasaida only on the onChanged event but it also needs to be set on the onTap event also so that it cannot be null across all Datepicker events even at empty value it will have "" string.

Here I have tested on singular TextFormField of type DateTime :

class AddSaida extends StatefulWidget {
const AddSaida({Key? key}) : super(key: key);
@override
State&lt;AddSaida&gt; createState() =&gt; _AddSaidaState();
}
class _AddSaidaState extends State&lt;AddSaida&gt; {
final _dateController = TextEditingController();
String? datasaida;
getSaidaName(datasaida) {
this.datasaida = datasaida;
}
createData() {
if (datasaida != null) {
DocumentReference ds = FirebaseFirestore.instance
.collection(&#39;addsaidas&#39;)
.doc(&quot;cnFxQ8wRivY39EywI3TS&quot;); //just a existing docId 
Map&lt;String, dynamic&gt; tasks = {
&quot;datasaida&quot;: datasaida,
};
ds.set(tasks).whenComplete(() {
print(&quot;task updated&quot;);
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
TextFormField(
controller: _dateController,
decoration: const InputDecoration(
labelText: &#39;Date&#39;,
),
onTap: () async {
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022),
lastDate: DateTime(2036),
);
if (pickedDate != null) {
_dateController.text =
DateFormat(&#39;dd-MM-yyyy&#39;).format(pickedDate);
getSaidaName(DateFormat(&#39;dd-MM-yyyy&#39;).format(pickedDate)); // ⇐ Notice
}
},
onChanged: (value) {
getSaidaName(value);
},
),
ElevatedButton(
onPressed: createData,
child: const Text(&#39;Save Date&#39;),
),
],
),
);
}
}
</details>

huangapple
  • 本文由 发表于 2023年2月16日 09:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定