我正在尝试让 GoogleSign 在 flutter 中使用 web 应用程序,为此我一直在关注一篇文章。这是他们说用于登录的功能:
Future<String> signInWithGoogle() async {
// Initialize Firebase
await Firebase.initializeApp();
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final UserCredential userCredential =
await _auth.signInWithCredential(credential);
final User user = userCredential.user;
if (user != null) {
// Checking if email and name is null
assert(user.uid != null);
assert(user.email != null);
assert(user.displayName != null);
assert(user.photoURL != null);
uid = user.uid;
name = user.displayName;
userEmail = user.email;
imageUrl = user.photoURL;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final User currentUser = _auth.currentUser;
assert(user.uid == currentUser.uid);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('auth', true);
return 'Google sign in successful, User UID: ${user.uid}';
}
return null;
}
它表示 SharedPreferences 是一个未定义的类。这是做什么的?有必要吗?如果是,我该如何解决这个问题?非常感谢您的帮助,因为这是我第一次在 flutter web 中使用 google 登录。
在您的
pubspec.yaml
文件中添加共享首选项依赖项。
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.0
shared_preferences: ^0.5.12+4
现在运行 flutter pug get。 像这样将包导入到您的文件中后,
import 'package:shared_preferences/shared_preferences.dart';
现在应该可以正常工作了。
pubspec.yaml
shared_preferences: ^0.5.12+4
dependencies:
flutter pub get
import 'package:shared_preferences/shared_preferences.dart';
shared_preferences
shared_preferences: ^2.2.3
依赖项:
扑:
sdk:颤动
谷歌字体:^6.2.1
material_design_icons_flutter:7.0.7296
共享首选项:^2.2.3import 'package:shared_preferences/shared_preferences.dart';
导入到您的 dart 项目中希望它有效。