我在玩flutter和SharedPreferences包
我已经建立了一个简单的项目来了解如何使用这个包,但我一吃午饭就得到了这个项目:
在通道上找不到方法getAll的实现plugins.flutter.io/shared_preferences
无法找出问题所在。
这是我的存储库:
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
abstract class AuthenticationRepository {
Future<User> getCurrentUser();
Future<User> signInWithEmailAndPassword(String email, String password);
Future<void> signOut();
}
class RestAuthenticationProvider extends AuthenticationRepository {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
var httpClient = new Dio();
@override
Future<User> getCurrentUser() async {
String token = await this.getToken();
if (null == token) {
return null;
}
final SharedPreferences prefs = await _prefs;
return User(
name: prefs.getString('user_name') ?? '',
email: prefs.getString('user_email') ?? '',
);
}
Future<String> getToken() async {
final SharedPreferences prefs = await _prefs;
String token = prefs.getString('user_token') ?? null;
return token;
}
@override
Future<User> signInWithEmailAndPassword(String email, String password) async {
Response response = await httpClient.post(
kIssueTokenUrl,
data: {
"email": email,
"password": password,
"device_name": 'Mobile app',
},
);
if (response.statusCode != 200) {
throw AuthenticationException(
message: 'Nome utente o password sbagliati.',
);
}
String token = response.data.toString();
httpClient.options.headers['content-Type'] = 'application/json';
httpClient.options.headers['Authorization'] = 'Bearer $token';
Response userInfoResponse = await httpClient.get(kGetUserInfoUrl);
if (userInfoResponse.statusCode != 200) {
throw AuthenticationException(
message: 'Nome utente o password sbagliati.',
);
}
String userName = userInfoResponse.data['name'];
String userEmail = userInfoResponse.data['email'];
final SharedPreferences prefs = await _prefs;
prefs.setString('user_token', token);
prefs.setString('user_name', userName);
prefs.setString('user_email', userEmail);
return User(
name: userName,
email: userEmail,
);
}
@override
Future<void> signOut() async {
final SharedPreferences prefs = await _prefs;
prefs.remove('token');
prefs.remove('user_name');
prefs.remove('user_email');
return null;
}
}
这是用户模型,仅供参考:
import 'package:meta/meta.dart';
class User {
final String name;
final String email;
User({
@required this.name,
@required this.email,
});
@override
String toString() => 'User { name: $name, email: $email}';
}
我只使用了getString
、setString
和remove
,不明白为什么要调用getAll并失败。
编辑:
Launching lib/main.dart on iPhone 11 Pro in debug mode...
Running pod install...
Running Xcode build...
Xcode build done. 19,3s
Failed to build iOS app
Could not build the application for the simulator.
Error launching application on iPhone 11 Pro.
Error output from Xcode build:
↳
** BUILD FAILED **
Xcode's output:
↳
/Users/christiangiupponi/Dev/FlutterApp/test/ios/Runner/GeneratedPluginRegistrant.m:10:9: fatal error: module 'shared_preferences' not found
@import shared_preferences;
~~~~~~~^~~~~~~~~~~~~~~~~~
1 error generated.
note: Using new build system
note: Planning build
note: Constructing build description
warning: Capabilities for Signing & Capabilities may not function correctly because its entitlements use a placeholder team ID. To resolve this, select a development team in the Runner editor. (in target 'Runner' from project 'Runner')
确保您使用的是最新版本(0.5.12+2(,然后运行flutter clean
和flutter pub get
。如果你在iOS上,也可以运行pod install
。您也可以尝试从手机中删除该应用程序。
如果错误仍然存在,请将SharedPreferences.setMockInitialValues({});
添加到main()
中。