Flutter - SharedPreferences 调用未定义的方法 getAll
Flutter - SharedPreferences call undefined method getAll
我在玩 flutter 和 SharedPreferences 包。
我已经建立了一个简单的项目来了解如何使用这个包,但是当我吃完这个项目时,我得到:
No implementation found for method getAll on channel
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;
}
}
这是User模型,仅供参考:
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
。您也可以尝试从 phone.
中删除该应用
如果错误仍然存在,请将 SharedPreferences.setMockInitialValues({});
添加到您的 main()
。
我在玩 flutter 和 SharedPreferences 包。
我已经建立了一个简单的项目来了解如何使用这个包,但是当我吃完这个项目时,我得到:
No implementation found for method getAll on channel 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;
}
}
这是User模型,仅供参考:
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
。您也可以尝试从 phone.
如果错误仍然存在,请将 SharedPreferences.setMockInitialValues({});
添加到您的 main()
。