Dynamically setting variable name in Dart

后端 未结 1 689
感情败类
感情败类 2021-01-26 19:33

I had saved a variables name and value to a JSON file in Dart. Later I extracted the name and value from that JSON file and now am trying to create a new variable with that name

相关标签:
1条回答
  • 2021-01-26 20:04

    Short answer: No.

    You cannot create variables at runtime in Dart. The compiler assumes that all variables are visible when the program (or any single method) is compiled.

    The way variables are looked up in Dart is that "x" refers to a local, static or top-level variable, if there is such a variable in the lexical scope, and it refers to "this.x" if there is variable in the lexical scope named "x".

    If you could add a variable later, you would be able to change "x" from meaning "this.x" to meaning something else. Already compiled code would then be incorrect.

    0 讨论(0)
提交回复
热议问题