您可以在didChangeDependencies中调用具有共享首选项的布尔值吗?
我需要在构建方法中使用isToolTipTapped
作为条件,但似乎不起作用。
@override
void didChangeDependencies() async {
isToolTipTapped = await checkIfToolTipTapped();
print('$isToolTipTapped');
super.didChangeDependencies();
}
Future<bool> checkIfToolTipTapped() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool(SchnapConfigValues.toolTipTapped ?? false);
}
print('$isToolTipTapped'); does return the boolean in didChangeDependencies method but my build method doesn't take the value
答案 0 :(得分:0)
我认为您应该尝试以下代码:
class _DemoPageState extends State<DemoPage> {
static const TAG = 'DemoPage';
bool isToolTipTapped;
Future<bool> checkIfToolTipTapped() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool(SchnapConfigValues.toolTipTapped ?? false);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
checkIfToolTipTapped().then((value) {
setState(() {
isToolTipTapped = value;
});
});
}
@override
Widget build(BuildContext context) {
if (isToolTipTapped == null) {
return Your Loading Widget...
} else {
//Handle logic with isToolTipTapped
}
}
}
答案 1 :(得分:0)
public IActionResult GetAssets(DateTime? ClosingDT = null, string InvestmentLevel = "child", string planCode = null, string PartyID = null)
{
try
{
var ContactInvBal = Repo.GetFund(this.UserToken.ContactID, ClosingDT, InvestmentLevel, planCode, PartyID);
decimal TotalAll = ContactInvBal.InvestmentBalances.Sum(b => b.ClosingBalance.HasValue ? b.ClosingBalance.Value : 0.00M);
var result = ContactInvBal.InvestmentBalances.Select(b => new ContactAssets()
{
Type = "",
AssetClass = b.AssetClass,
Balance = b.ClosingBalance,
Units = b.ClosingUnit,
Price = b.ClosingPrice,
PercentOfTotal = Convert.ToDecimal(b.ClosingBalance / TotalAll)
});
return Ok(result);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return BadRequest(ex.Message);
}
}