簡體   English   中英

斷言失敗:boolean 表達式不得為 null flutter

[英]Failed assertion: boolean expression must not be null flutter

我試圖讓開關 state 但它一直向我顯示此錯誤斷言失敗:boolean 表達式不能是 null

這就是我通過將開關 state 保存到共享偏好並取回價值來獲得它的方法

 bool availability; Future<bool> getSwitchState() async { SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() { availability = prefs.getBool('isAvailable')??false; }); } // and this how i save the switch state when it is changed Future<bool> savingSwitchValue(bool value) async { SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.setBool('isAvailable', value); } CustomSwitch( value: availability, onChanged: (value) { if (availability == false) { setState(() { availability = true; savingSwitchValue(value); print('this is true'); goOnline(); getLocationUpdates(); }); } else { setState(() { availability = false; savingSwitchValue(value); print('this is false'); goOffline(); }); } }, activeColor: Colors.green, ),
我該如何解決這個問題

您應該在第一行代碼中為可用性變量分配默認值。

它出錯了,因為您在為其賦值之前正在使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM