【发布时间】:2020-11-22 19:58:58
【问题描述】:
我正在处理仓库中的位置管理项目。 此代码检查可用空间。 但我对这段代码有疑问。
当我调用该函数时,它返回一个字符串,但返回“locatie is bezet”不起作用。 这是为什么呢?
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;
}
调用代码:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
width: 150,
child: TextFormField(
autofocus: true,
obscureText: false,
decoration: InputDecoration(
fillColor: Colors.white, filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(0)),
),
),
onSaved: (String value) {
_locatie = value;
},
validator: (value) {
var test = CheckLocation(value);
return test;
},
),
),
],
),
【问题讨论】:
-
CheckLocation 方法返回什么?
-
它返回一个带有荷兰语文本“此位置上已经有一个托盘”的字符串。
-
所有其他返回工作,但这个没有。它认为这是因为这个返回是在 if 语句中。
标签: function flutter dart return flutter-web