Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有什么方法可以检查是否在 dart 中为动态变量提供了 getter,而不是在 try catch 块中?
此处的示例 'ThisClass' 中未提供 'v1',因此会出错
class ThisClass{ bool v2=false; } main() { dynamic h=ThisClass(); print(h.v1); }
你可以做
class ThisClass { bool v2 = false; } main() { dynamic h = ThisClass(); if (h is ThisClass) { print(h.v2); } }
您可以尝试访问try块中的变量并捕获结果错误(如果它不存在):
try
try { print(h.v1); } catch (e) { // Handle the error }