调用对象字段作为 ListTile 属性
Calling an object field as ListTile attribute
我正在尝试从 json 文件中接收一些数据,然后将这些数据映射到我的 "Json" class 字段中。
有没有办法将我的 Json class 中的字段用作我的 ListTile 属性?
例如:
Json json1 = new Json();
...
const ListTile(
....
title: const Text (json1.fieldOne()),
...
以便 ListTile 属性可以从新对象的属性中读取,而不是为它们定义常量值。
显然我做错了什么
已将 const ListView
更改为 new ListView
以及其中的所有属性。
const
Dart 中的值是在编译时解析的表达式。
无法在 const 上下文中使用 new X()
创建的值(如 title: const Text(...)
)
我不太了解 Flutter,但我想不使用 const
应该也能正常工作
new ListTile(
....
title: new Text (json1.fieldOne()),
如果此代码嵌入到另一个 const
上下文中,您需要将 const
一直替换为 new
。
出于性能原因,const
在 Flutter 中尽可能多地使用,但如果不可能,就像您的情况一样,因为该值在编译时尚不可用,那么只需将其替换为 new
.
我正在尝试从 json 文件中接收一些数据,然后将这些数据映射到我的 "Json" class 字段中。
有没有办法将我的 Json class 中的字段用作我的 ListTile 属性?
例如:
Json json1 = new Json();
...
const ListTile(
....
title: const Text (json1.fieldOne()),
...
以便 ListTile 属性可以从新对象的属性中读取,而不是为它们定义常量值。
显然我做错了什么
已将 const ListView
更改为 new ListView
以及其中的所有属性。
const
Dart 中的值是在编译时解析的表达式。
无法在 const 上下文中使用 new X()
创建的值(如 title: const Text(...)
)
我不太了解 Flutter,但我想不使用 const
应该也能正常工作
new ListTile(
....
title: new Text (json1.fieldOne()),
如果此代码嵌入到另一个 const
上下文中,您需要将 const
一直替换为 new
。
const
在 Flutter 中尽可能多地使用,但如果不可能,就像您的情况一样,因为该值在编译时尚不可用,那么只需将其替换为 new
.