【问题标题】:String condition to bool要布尔的字符串条件
【发布时间】:2022-01-06 03:07:51
【问题描述】:

我正忙于尝试从后端字符串值在前端创建一个 if 语句

上图来自后端,我将 $blody1$ 替换为

所以最终它会是这样的 [Guest].length > 1

有没有办法将其转换为真或假,如果没有任何帮助的指针。我想将逻辑移到后端,但我相信我也会在那里遇到同样的问题。

旁注最终结果应该类似于 {"color" : "blue"} OR {"color" : "orange"}

我的代码

const start = "\$";
const end = "\$";

final startIndex = str.indexOf(start);
final endIndex = str.indexOf(end, startIndex + start.length);

String tValue =
    str.substring(startIndex + start.length, endIndex);
String tcondition = str.substring(endIndex + 1);
print(getValue(tValue).toString() + tcondition);

谢谢

【问题讨论】:

  • 无法将任意字符串评估为代码。这既不切实际,又是等待发生的任意代码执行漏洞。充其量您可以构建一个Map<String, dynamic> 将字符串"Guest" 映射到Guest 变量引用的对象,然后在解析传入的字符串时使用它。
  • 明白,我需要再调查一下...谢谢

标签: flutter dart dynamic backend dynamic-programming


【解决方案1】:

你可以检查下面的代码

void main() {
   var data = {"body1":["Guest"]};
  
  print(data["body1"][0]=="Guest");//true
}

更新

检查身体长度

Color color = Colors.green;
 if(data["body1"].length>1){
   // set your color
   color = Colors.red;
 } 

【讨论】:

  • 它是动态的,所以有时会是 ["Guest", "Admin"] 等等。代码背后的想法是最终它将是{“color”:“green”}或{“color”:“orange”}。在条件下它也可以改变。例如,它可能类似于 userId == "27234"
  • 你明白了,但是你改变了一个不同的角色可以手动或动态地改变它
  • 它将被动态填充
  • const start = "\$";常量结束 = "\$";最终 startIndex = str.indexOf(start);最终 endIndex = str.indexOf(end, startIndex + start.length); String tValue = str.substring(startIndex + start.length, endIndex);字符串 tcondition = str.substring(endIndex + 1); print(getValue(tValue).toString() + tcondition);
  • 上面的代码是我使用的,但发现它没有像预期的那样工作
猜你喜欢
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 2010-10-15
  • 2013-06-13
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 2014-03-30
相关资源
最近更新 更多