我正在制作一个表单,供用户通过输入卖家的帐户名称和帐户类型来添加新卖家。但在将新卖家添加到firestore之前,它会检查该卖家是否存在。现在我的代码仅检查输入的名称是否与数据库中的名称非常相似。
例如:数据库中有SellerABC,那么如果用户输入,
- sellerABC = 不相同
- seLLerAbc = 不相同
- 卖家ABC = 相同
由于卖家的账户名可能是大写和小写的组合,我想检查两个字符串的相似字母。如果用户输入的名称与数据库中的字母相同,则返回 true。
例如:
我应该如何检查字符串中是否有相同的字母?
这是我的代码:
Future addSeller({
required String seller_name,
required String? typeOfAcc,
}) async {
final docSeller = FirebaseFirestore.instance.collection('sellers').doc();
final querySnapshot = FirebaseFirestore.instance
.collection('sellers')
.where('seller_name', isEqualTo: seller_name)
.where('account_type', isEqualTo: typeOfAcc);
querySnapshot.get().then((querySnapshot) async {
if (querySnapshot.docs.isNotEmpty) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: Text('Error'),
content: Text('This seller already exists!'),
actions: <Widget>[
TextButton(
child: Text(
'OK',
style: TextStyle(color: Colors.white),
),
onPressed: () {
Navigator.of(context).pop();
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Color.fromARGB(255, 43, 115, 255))),
),
],
));
} else {
final json = {
'seller_name': seller_name,
'account_type': typeOfAcc,
};
await docSeller.set(json);
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
title: Text('Successful'),
content: Text('New seller has been successfully added!'),
actions: <Widget>[
TextButton(
child: Text(
'OK',
style: TextStyle(color: Colors.white),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EvaluateCriteria(
sellerName: seller_name,
typeAcc: typeOfAcc)));
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Color.fromARGB(255, 43, 115, 255))),
),
],
));
}
});
}
}