gpt4 book ai didi

firebase - 如何使用表单验证通过Flutter验证集合Firestore中是否存在文档

转载 作者:行者123 更新时间:2023-12-03 03:58:38 25 4
gpt4 key购买 nike

我想使用表单验证来验证Firestore集合内的文档是否确实存在抖动。否则,我会向用户返回带有错误消息的字符串。用户只能在填写数据库中已经存在的名称后继续操作(否则,他们将无法订阅正确的环境)。
我在代码中使用了带有Form和验证器的TextFormField

下面我将stateful widget包含在Form

import 'package:flutter/material.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';

class BuildingScreen extends StatefulWidget {

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

class _BuildingScreenState extends State<BuildingScreen> {
final _formKey = GlobalKey<FormState>();
bool showSpinner = false;
String environment;
bool _autoValidate = false;

void _validateInputs() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
setState(() {
showSpinner = true;
});
} else {
setState(() {
_autoValidate = true;
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: ModalProgressHUD(
inAsyncCall: showSpinner,
child: Column(
children: <Widget>[
Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
textAlign: TextAlign.center,
validator: validateEnvironment,
onSaved: (String value) {
environment = value;
},
),
RoundedButton(
// the RoundedButton is just a material button with some custom padding
buttonTitle: 'Verify',
onPressed: () async {
_validateInputs();

try {
if (_formKey.currentState.validate()) {
Navigator.pushNamed(
context, RoosterEnvironment.id);
}
setState(() {
showSpinner = false;
});
} catch (e) {
print(e);
}
},
),
],
),
),
],
),
),
);
}
}


下面是validateEnvironment函数:
import 'package:cloud_firestore/cloud_firestore.dart';

String validateEnvironment(String value) {
if (value != Firestore.instance.collection('environments').document(value).documentID)
return 'Environment not on the list';
else
return null;
}

在我的Firestore数据库中,我有一个名为“环境”的集合。在此集合中,我保存了一个名为“test”的文档,其字段为“name:test”。现在 (value != Firestore.instance.collection('environments').document(value).documentID)始终为false,因为firestore实例返回 TextFormField中插入的任何值。当我将其替换为 (value != 'test')时,验证程序运行正常。我也尝试过命名一个集合“test”,然后验证它是否像这样存在: (value != Firestore.instance.collection('test')),但是返回一个“collectionReference”的实例。

我想知道是否有可能进行表单验证检查,还是应该以完全不同的方式进行检查?任何帮助,将不胜感激!

更新

多亏了一些帮助,我才更新了代码。现在,我使用 TextEditingController和G Griffo的代码来检查Firestore中是否存在文本字段中的值。当还不存在错误消息时,我还没有添加错误消息,但是下面我提供了适用于我的代码。
final _controller = TextEditingController();

static Future<bool> validateEnvironment(String docID) async {
bool exists = false;
try {
await Firestore.instance
.document("environments/$docID")
.get()
.then((doc) {
if (doc.exists)
exists = true;
else
exists = false;
});
print(exists);
return exists;
} catch (e) {
return false;
}
}

void _formvalidate(String docId) async {
validateEnvironment(docId).then((value) {
if (value == true) {
_updateData();
Navigator.pushNamed(context, RoosterEnvironment.id);
} else {
// do something
}
});
}


onPressed中,我仅包含以下内容:
onPressed: () {
environment = _controller.text;
_formvalidate(environment);
},

再次感谢!

最佳答案

您可以尝试通过运行查询来搜索文档IDS来检查您的ID是否已存在于该集合中。我们从执行该操作的静态方法开始

     static Future<bool> validateEnvironment(String docID) async {
bool exists = false;
try {
await Firestore.instance.document("environments/$docID").get().then((doc) {
if (doc.exists)
exists = true;
else
exists = false;
});
return exists;
} catch (e) {
return false;
}
}

最后从您的类中调用该函数
 void  _formvalidate(String docId)async{
validateEnvironment(docId).then((value) {
if (!value) {
//file not found do dome stuff
....
} else {
//document exists do some stuff
}
});
}

关于firebase - 如何使用表单验证通过Flutter验证集合Firestore中是否存在文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59036023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com