颤振版本:
我遇到了一个例外SharedPreferences prefs = await SharedPreferences.getInstance();
。
Exception has occurred.
_TypeError (type '() => Null' is not a subtype of type '(dynamic) => dynamic' of 'f')
相同的代码在登录功能中正常工作。
代码:
appBar: AppBar(
title: const Text('Messanger Clone'),
actions: <Widget>[
InkWell(
onTap: () {
AuthMethods().signOut().then(() {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => SignIn()));
});
},
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: const Icon(Icons.exit_to_app),
),
),
],
),
class SharedPreferenceHelper{
Future<bool> saveUserId(String userId) async {
SharedPreferences prefs = await SharedPreferences.getInstance(); //Working
return prefs.setString(userIdKey, userId);
}
}
class AuthMethods {
//Other Methods
signOut() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance(); // Not working
prefs.clear();
await auth.signOut();
} on Exception catch (error) {
if (kDebugMode) {
print('Exception : $error');
}
}
}
}
Flutter 2.8.1 • 通道稳定 • https://github.com/flutter/flutter.git
框架 • 修订版 77d935af4d(4 周前) • 2021-12-16 08:37:33 -0800
引擎 • 修订版 890a5fca2e
工具 • Dart 2.15.1