显示存储在 Flutter Secure Storage 中的用户详细信息时出错

Error displaying user details stored in Flutter Secure Storage

我一直在尝试在我的个人资料 class 中显示 Registration/Login 期间获取的用户详细信息,但无法显示。仅当调整浏览器 Window 大小时或提供输入(例如将图像上传到个人资料)时,才会显示详细信息。注册或登录后不会自动显示详细信息。

屏幕代码如下:

Future<Map<String, String>> getDetails() async {
  final Map<String, String> _details = {
    'firstname': '',
    'lastname': '',
    'username': '',
    'bio': '',
  };
  
  var storage = const FlutterSecureStorage();
  _details['username'] = (await storage.read(key: 'username')).toString();
  _details['firstname'] = (await storage.read(key: 'firstname')).toString();
  _details['lastname'] = (await storage.read(key: 'lastname')).toString();
  _details['bio'] = (await storage.read(key: 'bio')).toString();
  return _details;
}

class ProfileInfo extends StatefulWidget {
  const ProfileInfo({Key? key}) : super(key: key);


  @override
  State<ProfileInfo> createState() => _ProfileInfoState();
}

class _ProfileInfoState extends State<ProfileInfo> {
  static String url = 'assets/artist_avatar.png';

  Map<String, String> details = {
    'firstname': '',
    'lastname': '',
    'username': '',
    'bio': '',
  };

  @override
  void initState() {
    Future.delayed(Duration.zero,() async {
        details = await getDetails();
    });
    super.initState();
  }

显示详情的代码同class:

Text(details['firstname']!, //----------->displaying name
                style: const TextStyle(        
                  fontSize: 18.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                )),
            Text(details['username']!,   //----------->displaying username
                style: const TextStyle(
                  color: Colors.black45,
                  fontWeight: FontWeight.w100,
                )),

尝试调用 setState

像这样:

@override
void initState() {
  Future.delayed(Duration.zero,() async {
     details = await getDetails();
     setState((){});
  });
  super.initState();
}