Я пытаюсь разработать приложение Flutter с несколькими страницами. У меня есть SignInService, который я пытаюсь вставить на разные страницы с помощью локатора службы get_it.
ServiceLocator.dart
final locator = GetIt.instance;
void setupLocator() {
locator.registerSingleton<SignInService>(SignInService());
locator.registerSingleton<LocationService>(LocationService());
}
SignInService.dart
class SignInService {
GoogleSignInAccount googleAccount;
FirebaseUser firebaseUser;
FirebaseAuth _auth = FirebaseAuth.instance;
.
.
}
Когда я войдите в систему, переменная firebaseUser устанавливается в этом классе, и значение не равно нулю.
Когда я использую ее на другой странице, я получаю ноль.
E / flutter (20069): [ОШИБКА: flutter / lib / ui / ui_dart_state. cc (157)] Необработанное исключение: NoSuchMethodError: Получатель uid был вызван с нулевым значением. E / flutter (20069): Получатель: null E / flutter (20069): Пробовал позвонить: uid
AnotherPage.dart
class _AnotherPageState extends State<AnotherPage> {
final _signInService = locator<SignInService>();
Firestore _firestore = Firestore.instance;
_getUserCity() async {
DocumentSnapshot snapshot = await _firestore
.collection('users')
.document(_signInService.firebaseUser.uid) //throws uid called on null error
.get();
_city = snapshot.data["city"];
}
_getPackage() async {
_getUserCity();
.
.
}
@override
initState(){
super.initState();
_getPackage();
}
@override
Widget build(BuildContext context) {
return Scaffold(
//...
);
}