我想將 widget.token 發送到 User 建構式,但我不能,它顯示了這個錯誤:
無法在初始化程式中訪問實體成員“小部件”。嘗試用不同的運算式替換對實體成員的參考
這是我的代碼:
class HomeScreen extends StatefulWidget {
final String token;
const HomeScreen(this.token, {Key? key}) : super(key: key);
@override
createState() {
return HomeScreenState();
}
}
@override
class HomeScreenState extends State<HomeScreen> {
int _selectedIndex = 2;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static final List<Widget> _widgetOptions = <Widget>[
User(widget.token),
const Profile(),
News(),
SingleChildScrollView(
child: Search(),
),
ChartAl('null', 'Line'),
// Home(),
];
@override
Widget build(BuildContext context) {
.....
...
...
..
uj5u.com熱心網友回復:
簡單地說,您不能用于widget初始化狀態的屬性。
您應該在initState, 或build函式中訪問它。另一種可能:
List<Widget> get _widgetOptions => <Widget>[
User(widget.token),
const Profile(),
News(),
SingleChildScrollView(
child: Search(),
),
ChartAl('null', 'Line'),
// Home(),
];
此外,您永遠無法在靜態背景關系中訪問實體變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362376.html
上一篇:在SQL中將字串轉換為日期
