게시하기 전에 이전 질문을 살펴 보았지만 (많은 이유 때문에) 내 필요에 맞는 것을 찾지 못했습니다.
Firestore에 문서가 있는지 여부를 확인하는 함수가 있는데 문서가 존재하면 함수는 false를 반환해야합니다. 그렇지 않으면 존재하지 않으면 true를 반환해야합니다.
문제는 함수의 반환이 항상 null이고 컴파일러가 함수에 return 문이 없다고 말했지만 이유를 이해할 수 없다는 것입니다.
이것은 코드이고 중요한 기능은 checkMissingId
다른 하나가 문자열 id
이 유효한 형식인지 여부를 확인하는 것입니다.
코드 :
bool checkStr(String id, String letter, String str) {
if (id.length < 1) {
print("Id is too short");
return false;
} else {
if ('a'.codeUnitAt(0) > letter.codeUnitAt(0) ||
'z'.codeUnitAt(0) < letter.codeUnitAt(0)) {
print("User name begins with bad word!");
return false;
}
print("ids/tabs/" + letter);
return true;
}
}
Future<bool> checkMissingId(String id, context) async {
String str = id.toLowerCase();
String letter = str[0];
if (checkStr(id, letter, str) == false)
return false; //checks some rules on strings
else {
try {
await FirebaseFirestore.instance.collection("ids/tabs/" + letter).doc(str).get()
.then((DocumentSnapshot documentSnapshot) { //Maybe here!(??)
if (documentSnapshot.exists) {
print("Document exists!");
return false;
} else {
print('Document does not exist on the database');
return true;
}
});
} catch (e) {
await showErrDialog(context, e.code);
return false;
}
}
}
이 시도:
Future<bool> checkMissingId(String id, context) async {
String str = id.toLowerCase();
String letter = str[0];
if (checkStr(id, letter, str) == false)
return false; //checks some rules on strings
else {
try {
var data = await FirebaseFirestore.instance.collection("ids/tabs/" + letter).doc(str).get()
if (data.exists) {
print("Document exists!");
return false;
} else {
print('Document does not exist on the database');
return true;
}
} catch (e) {
await showErrDialog(context, e.code);
return false;
}
}
}
문제는 .then(...)
함수에서 함수를 입력으로 취한다는 것입니다. 따라서 아무것도 반환 할 수 없습니다. 함수에 데이터를 반환하지 않기 때문입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다