如何在“initState”方法中等待SecureStorage?

huangapple go评论63阅读模式
英文:

How to wait for SecureStorage in "initState" method?

问题

我有以下代码在我的HomeScreen中:

HomeScreen():

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
  int requestCount = 0;
  int selectedIndex = 0;
  List<Widget> listWidgets = [
    const ChatsScreen(),
    const PairedScreen(),
    const PeopleScreen(),
    const GroupsScreen(),
    const RequestsScreen()
  ];

  //For tab selection
  void onItemTapped(int index) {
    setState(() {
      selectedIndex = index;
    });
  }

  String? username;

  getUsername() async {
    username = await SecureStorage().readSecureStorage('username');
  }

  @override
  void initState() {
    super.initState();

    getUsername();
    //Initialize Socket.IO
    if (username != null) {
      SocketIO().initSocket(username!);
    }
  }
  .
  .
  .
}

我需要等待SecureStorage获取存储在设备上的用户名(用户成功登录时先前存储的用户名),然后将其发送到Socket.IO服务器,以便在数据库中注册相关用户的socketID。
当我运行这段代码时,username是null,因为initState()不会等待SecureStorage。如何修复这个问题?

请确保在调用initSocket之前等待getUsername完成。你可以在initState中使用await关键字来等待getUsername方法的完成,如下所示:

@override
void initState() {
  super.initState();
  initializeSocket();
}

void initializeSocket() async {
  await getUsername();
  if (username != null) {
    SocketIO().initSocket(username!);
  }
}

这样,initSocket将在getUsername完成后才被调用,确保username不为null。

英文:

I have the following code in my HomeScreen:

HomeScreen():

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State&lt;HomeScreen&gt; createState() =&gt; _HomeScreenState();
}
class _HomeScreenState extends State&lt;HomeScreen&gt; {
  int requestCount = 0;
  int selectedIndex = 0;
  List&lt;Widget&gt; listWidgets = [
    const ChatsScreen(),
    const PairedScreen(),
    const PeopleScreen(),
    const GroupsScreen(),
    const RequestsScreen()
  ];

//For tab selection
  void onItemTapped(int index) {
    setState(() {
      selectedIndex = index;
    });
  }

  String? username;

  getUsername()async{
    username = await SecureStorage().readSecureStorage(&#39;username&#39;);
  }

  @override
  void initState() {
    super.initState();

    getUsername();
    //Initialize Socket.IO
if (username != null) {
  SocketIO().initSocket(username!);
}
  }
.
.
.
}

I need to wait for SecureStorage to get username stored in the device (stored previously when the user has logged in successfully) and send it to socket.IO server in order to register socketID of the related user in the database.
When I run this code, username is null because initState() does not wait for SecureStorage. How can i fix this?

答案1

得分: 2

您可以将SecureStorage的初始化移到一个单独的函数中,并在该函数内使用await来等待它完成。这样,您可以在该函数内使用await语句来确保它在继续之前完成。这里是一个示例:

@override
void initState() {
  super.initState();
  initFunction();
}

Future<void> initFunction() async {
  username = await SecureStorage().readSecureStorage('username');
  if (username != null) {
    SocketIO().initSocket(username!);
  }
}
英文:

You can move the securestorage initialisation to a separate function and use an await inside that function to wait for it
This way, you can utilize an await statement within that function to ensure that it completes before proceeding. Here's an example:

@override
void initState() {
  super.initState();
  initFunction();
}

Future&lt;void&gt; initFunction() async {
  username = await SecureStorage().readSecureStorage(&#39;username&#39;);
  if (username != null) {
    SocketIO().initSocket(username!);
  }
}

huangapple
  • 本文由 发表于 2023年5月11日 02:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定