【发布时间】:2021-08-23 21:27:35
【问题描述】:
我需要从 SharedPreferences 中获取一个数字并将其存储在一个常量中。但是我做不到。
SharedPreferences sp = await SharedPreferences.getInstance();
const int index = sp.getInt('index')!;
此代码抛出此错误:
Const variables must be initialized with a constant value.
Try changing the initializer to be a constant expression.
我想获取一次数据,以后不再更改。我怎样才能做到这一点?
【问题讨论】:
-
你不能 const 在编译时应该是常量,并且从共享首选项中检索到的值在编译时是未知的。
标签: flutter dart constants flutter-sharedpreference