我在登录块中使用flutter_secure_storage包来在登录后安全地保存用户数据。但是,处理在调用secure_storage 来存储数据时卡住了。在服务器上登录工作正常。当我再次注释掉本地存储代码时,线程工作正常。
这是在我的登录区
...
else if (event is LoginSubmitted) {
yield state.copyWith(appStatus: Submitting()); //show loading icon
//make API request to node server
try{
final response = await authRepo.signIn(userName: state.userName, password: state.password);
final responseCode = response.statusCode;
if (responseCode == 200) {
var responseJson = json.decode(response.body);
//print(responseJson); //reponse json prints in the console
UserModel = UserModel.fromJson(responseJson);
//store user data locally securely. This where am getting blocked.
await UserSecureStorage.setToken(user.data.token);
await UserSecureStorage.setUserId(user.data.user.id);
...
yield state.copyWith(appStatus: Success());
}
}
UserSecureStorage 类
import 'dart:async';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class UserSecureStorage {
static final _storage = FlutterSecureStorage();
static Future setToken(String token) async {
return await _storage.write(key: 'token', value: token);
}
static Future getToken() async {
final token = await _storage.read(key: 'token');
return token;
}
static Future setUserId(String id) async {
return await _storage.write(key: 'userId', value: id);
}
static Future getUserId() async {
final id = await _storage.read(key: 'userId');
return id;
}
static Future setUserName(String userName) async {
return await _storage.write(key: 'username', value: 'userName');
}
static Future getUserName() async {
final username= await _storage.read(key: 'userName');
return username;
}
}
请求堆栈在 ***await
UserSecureStorage.setToken(user.data.token);...
在登录区。我究竟做错了什么?