英文:
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<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!);
}
}
.
.
.
}
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<void> initFunction() async {
username = await SecureStorage().readSecureStorage('username');
if (username != null) {
SocketIO().initSocket(username!);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论