Function 在函数中返回字符串
我正在做一个仓库位置管理的项目。 此代码检查可用空间。 但是我对这个代码有一个问题 当我调用函数时,它返回一个字符串,但返回“locatie is bezet”不起作用。 为什么呢Function 在函数中返回字符串,function,flutter,dart,return,flutter-web,Function,Flutter,Dart,Return,Flutter Web,我正在做一个仓库位置管理的项目。 此代码检查可用空间。 但是我对这个代码有一个问题 当我调用函数时,它返回一个字符串,但返回“locatie is bezet”不起作用。 为什么呢 import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:transportmanagement/locations/AvailableLocations.dart'; final firestoreInstance = Fireb
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:transportmanagement/locations/AvailableLocations.dart';
final firestoreInstance = FirebaseFirestore.instance;
var locations = availableLocations;
Future<String> CheckLocation(Location_check) async{
// This function checks the avalible space in SGF.
String checkLocation;
if (Location_check.isEmpty){
checkLocation= "Vul een locatie in";
// return "Vul een locatie in";
}
else{
if (availableLocations.containsKey(Location_check)) {
// Check if the location is in availablelocations.dart
// Checks if there is already a pallet on this location in Firebase
await firestoreInstance.collection("Orders").where("locatie", isEqualTo: Location_check).limit(1).get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
//print(result.data());
checkLocation= "Locatie is bezet";
});
});
// return null;
}
else{
checkLocation= "Locatie bestaat niet";
// return "Locatie bestaat niet";
}
}
return checkLocation;
}
在函数中定义一个字符串变量,然后返回该变量
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:transportmanagement/locations/AvailableLocations.dart';
final firestoreInstance = FirebaseFirestore.instance;
var locations = availableLocations;
String CheckLocation(Location_check) {
// This function checks the avalible space in SGF.
String checkLocation;
if (Location_check.isEmpty){
checkLocation= "Vul een locatie in";
// return "Vul een locatie in";
}
else{
if (availableLocations.containsKey(Location_check)) {
// Check if the location is in availablelocations.dart
// Checks if there is already a pallet on this location in Firebase
firestoreInstance.collection("Orders").where("locatie", isEqualTo: Location_check).limit(1).get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
//print(result.data());
checkLocation= "Locatie is bezet";
});
});
// return null;
}
else{
checkLocation= "Locatie bestaat niet";
// return "Locatie bestaat niet";
}
}
return checkLocation;
}
我希望这是有帮助的
更新
querySnapshot.docs.forEach
excite after returncheckLocation
变量
所以返回null
您必须将wait
添加到firestoreInstance.collection(“Orders”).where(“locatie”,isEqualTo:Location\u check).limit(1).get()
看到了吗
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:transportmanagement/locations/AvailableLocations.dart';
final firestoreInstance = FirebaseFirestore.instance;
var locations = availableLocations;
String CheckLocation(Location_check) {
// This function checks the avalible space in SGF.
String checkLocation;
if (Location_check.isEmpty){
checkLocation= "Vul een locatie in";
// return "Vul een locatie in";
}
else{
if (availableLocations.containsKey(Location_check)) {
// Check if the location is in availablelocations.dart
// Checks if there is already a pallet on this location in Firebase
await firestoreInstance.collection("Orders").where("locatie", isEqualTo: Location_check).limit(1).get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
//print(result.data());
checkLocation= "Locatie is bezet";
});
});
// return null;
}
else{
checkLocation= "Locatie bestaat niet";
// return "Locatie bestaat niet";
}
}
return checkLocation;
}
什么是CheckLocation方法return?它返回一个带有荷兰语文本“此位置上已存在托盘”的字符串。所有其他方法返回工作,但此方法不返回。它认为这是因为这个返回包含在if语句中。谢谢你的帮助!你的代码看起来不错。但当我调用此代码时,它也会返回所有检查位置,但“Locatie is bezet”返回null。这意味着不满足条件,或者快照中没有文档打印documentadd wait before
firestoreInstance.collection(“Orders”)。where(“Locatie”,isEqualTo:Location\u check)。limit(1)。get()
我在“返回checkLocation”之前添加了“print(checkLocation)”,结果为空。但当我在“checkLocation=“Locatie is bezet”下面添加“print”(“test”)时,结果是test。因此,该代码触发print语句。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:transportmanagement/locations/AvailableLocations.dart';
final firestoreInstance = FirebaseFirestore.instance;
var locations = availableLocations;
String CheckLocation(Location_check) {
// This function checks the avalible space in SGF.
String checkLocation;
if (Location_check.isEmpty){
checkLocation= "Vul een locatie in";
// return "Vul een locatie in";
}
else{
if (availableLocations.containsKey(Location_check)) {
// Check if the location is in availablelocations.dart
// Checks if there is already a pallet on this location in Firebase
await firestoreInstance.collection("Orders").where("locatie", isEqualTo: Location_check).limit(1).get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
//print(result.data());
checkLocation= "Locatie is bezet";
});
});
// return null;
}
else{
checkLocation= "Locatie bestaat niet";
// return "Locatie bestaat niet";
}
}
return checkLocation;
}