我不明白UniqueKey如果編譯器不允許我們應該如何將 a 分配給一個物件。
如果我使用第二個選項,此代碼將失敗:
body: const Center
(
child: Note(key: ValueKey('note'), initialValue: 'test'), // WORKS
child: Note(key: UniqueKey(), initialValue: 'test'), // ERROR
),
class Note extends StatefulWidget
{
final String initialValue;
const Note({required Key key, this.initialValue = ""}) : super(key: key);
給出這個錯誤:
Error: Cannot invoke a non-'const' constructor where a const expression is
expected.
Try using a constructor or factory that is 'const'.
我該如何更改以允許 UniqueKey 作業?
uj5u.com熱心網友回復:
您需要從const代碼中洗掉,因為 const 需要一個常量,而 UniqueKey() 會改變自己。
body: Center
(
child: Note(key: ValueKey('note'), initialValue: 'test'), // WORKS
child: Note(key: UniqueKey(), initialValue: 'test'), // ERROR
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372201.html
標籤:扑
