为什么我不能在颤振中接收标题变量?

Was*_*oth 3 android dart flutter

在 flutter 中,您无法设置title要测试的变量,因为它Text不是常量。

class StockCard extends StatelessWidget {

  String test;

  StockCard(String t) {
    test = t;
  }

  @override
  Widget build(BuildContext context) {
    return (
      new Container(
        margin: const EdgeInsets.all(10.0),
        child: new Card(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              const ListTile(
                leading: Icon(Icons.trending_up),
                title: Text(test), // ERROR: error: Evaluation of this constant expression throws an exception.
                subtitle: Text('Apple Incorporated'),
              ),
            ],
          ),
        ),
      )
    );
  }

}
Run Code Online (Sandbox Code Playgroud)

我将如何实现这一点,为什么颤振不允许这种情况发生?

Rém*_*let 8

发生这种情况是因为您尝试将父级实例ListTile化为const.

但是对于ListTilebe const,它也要求它的孩子Text也是 const ,因此不能有一个变量。只要改变ListTileconst

ListTile(
   ...
)
Run Code Online (Sandbox Code Playgroud)