我是 flutter flow 和 Dart 的新手,在使用自定义操作时,我在操作流编辑器中链接到我的小部件,一切都很顺利,直到我发现异常行为(我的程序中没有可见的错误):
这是我在 flutter flow 中使用的代码/自定义操作:-
import 'dart:convert';
// This is only for clarification of arguments passed...
void main(){
foreSupportCalc(
12, //from textinput
45, //from textinput
"20 guage", //from dropdown
5, //from counter
6, //from textinput
"14 guage", //from dropdown
1, //from counter
350, //from AppState
1500, //from ApSate
'{"14": 2, "16": 1.5, "20": 1}', //from ApSate
'{"14": 175, "16": 150, "18": 130}'); //from AppState
}
// Main Custom Action starts here
Future<String> foreSupportCalc(
double shelfWidth,
double shelfLength,
String shelfGuage,
int shelfQuantity,
double angleHeight,
String angleGuage,
int rackQuantity,
double metalRates,
double labourRates,
String shelfGuageMap,
String angleGuageMap,
) async {
if (shelfWidth != null &&
shelfLength != null &&
shelfGuage != null &&
shelfQuantity != null &&
angleHeight != null &&
angleGuage != null &&
rackQuantity != null) {
shelfWidth += 3;
shelfLength += 3;
Map<String, dynamic> shelfGuages = json.decode(shelfGuageMap);
Map<String, dynamic> angleGuages = json.decode(angleGuageMap);
double shelfGuage_mm = shelfGuages[shelfGuage.replaceAll(" guage", "")];
double angleGuage_rate = angleGuages[angleGuage.replaceAll(" guage", "")];
// Shelf Rates
double perShelfRate = (((shelfWidth * shelfLength) / 144) *
shelfGuage_mm * metalRates)
.roundToDouble();
double totalShelfRate = perShelfRate * shelfQuantity;
double totalShelfRateForMultiRack = totalShelfRate * rackQuantity;
int multiShelfQty = shelfQuantity * rackQuantity;
// Angle Rates
double perAngleRate = (angleGuage_rate * angleHeight).roundToDouble();
double totalAngleRate = perAngleRate * 4;
double totalAngleRateForMultiRack = totalAngleRate * rackQuantity;
int multiAngleQty = 4 * rackQuantity;
// Total Rack Cost
double perRackCost = totalShelfRate + totalAngleRate + labourRates;
double totalRackCost = perRackCost * rackQuantity;
// Labour Rates
double multiLabourRates = labourRates * rackQuantity;
// Rates as String
Map<String, dynamic> outputAns;
if (rackQuantity > 1) {
outputAns = {
'Per Shelf Rates': perShelfRate,
'Per Angle Rates': perAngleRate,
'_+': '-:-',
'Shelf Rates ($multiShelfQty)': totalShelfRateForMultiRack,
'Angle Rates ($multiAngleQty)': totalAngleRateForMultiRack,
'Labour, etc Cost': multiLabourRates,
'__+': '-:-',
'Rates Per Rack': perRackCost,
'Rate for $rackQuantity Racks': totalRackCost
};
} else {
outputAns = {
'Per Shelf Rates': perShelfRate,
'Per Angle Rates': perAngleRate,
'-': '-',
'Shelf Rates ($shelfQuantity)': totalShelfRate,
'Angle Rates (4)': totalAngleRate,
'Labour, etc Cost': labourRates,
'-:': '-',
'Rates Per Rack': perRackCost,
};
}
String result = '';
// Iterate over map entries
outputAns.forEach((key, value) {
// Concatenate each key-value pair into the string
result += '$key: $value\n';
});
result = result.replaceAll("_+:", "");
result = result.replaceAll("__+:", "");
return result;
} else {
return "Error: Something is missing!";
}
}
这应该是我在警报对话框中传递的此操作的输出:-
Per Shelf Rates: Rs.1750
Per Angle Rates: Rs.1050
-:-
Shelf Rates (5): Rs.8750
Angle Rates (4): Rs.4200
Labour, etc Cost: Rs.1500
-:-
Rates Per Rack: Rs.14450
我尝试操作代码并使用更简单的测试代码,例如仅返回示例字符串,在这种情况下,该代码在所有平台上都可以正常工作,但此代码并非如此。我还仔细检查了 AppState 变量的数据类型,这些都没有问题。
如果它在 Debug 中可以工作,那为什么在 android 中不行呢?
编辑:使用网络日志后
Ctrl+Shift+j
在我的控制台上看到了这个:
Uncaught TypeError: J.bV(...).aj is not a function
at Object.eO (main.dart.js:29145:24)
at Object.ayg (main.dart.js:5764:3)
at aN2.$1 (main.dart.js:88145:19)
at bEg (main.dart.js:4101:17)
at main.dart.js:4109:51
解答 (以后如果有人遇到同样的问题)
谢谢大家没有回复我的请求,但是我发现导致此错误的问题就在这行代码中:
String shelfGuageMap = '{"14": 2, "16": 1.5, "20": 1}';
String angleGuageMap = '{"14": 175, "16": 150, "18": 130}';
Map<String, dynamic> shelfGuages = json.decode(shelfGuageMap);
Map<String, dynamic> angleGuages = json.decode(angleGuageMap);
似乎 flutter flow 不支持 android 上的 String 到 Map 转换,或者至少在我的情况下不支持,所以我将整个逻辑更改为:
String shelfGuageList = '["11:3", "12:2.5", "13:2.5", "14:2"]';
String angleGuageList = '["11:140", "12:150", "13:160", "14:175"]';
List<String> shelfGuageToList = json.decode(shelfGuageList).cast<String>();
List<String> angleGuageToList = json.decode(angleGuageList).cast<String>();
现在,不知道为什么这是一个问题,但代码终于可以在所有设备上运行了。