【发布时间】:2023-01-11 20:47:12
【问题描述】:
我的脚手架中有一个抽屉菜单,我想显示一些来自 Flutter 安全存储的信息。
class DrawerMenu extends StatefulWidget {
final Translations translations;
final PageController controller;
const DrawerMenu({
Key? key,
required this.translations,
required this.controller,
}) : super(key: key);
@override
State<DrawerMenu> createState() => _DrawerMenuState();
}
String? name;
String? email;
final FlutterSecureStorage storage = FlutterSecureStorage();
class _DrawerMenuState extends State<DrawerMenu> {
@override
void initState() {
getInfo();
super.initState();
}
getInfo() async {
name = await storage.read(key: 'name');
email = await storage.read(key: 'email');
}
@override
Widget build(BuildContext context) {
Translations translations = Translations.of(context);
return Drawer(
backgroundColor: AppColors.secondaryColor,
child: SafeArea(
bottom: false,
child: Column(
children: [
ClipOval(
child: Container(
color: AppColors.primaryColor,
height: 60.0,
width: 60.0,
child: Center(
child: Text(
name![0],
style: TextStyle(
color: AppColors.secondaryColor,
fontSize: 30,
fontWeight: FontWeight.bold),
),
),
),
),
);
}
}
我第一次遇到这个错误:_CastError(用于空值的空检查运算符)但是,如果我尝试下一步并重新打开抽屉,那就完成了!
我想看看名称在我的抽屉菜单中。
【问题讨论】:
-
问题是在接收到数据之前正在呈现 UI。所以为了避免这种情况,在 SchedulerBinding 中添加 getInfo。像:SchedulerBinding.instance.addPostFrameCallback({getInfo();});