I am trying to develop a Flutter app with multiple pages. I have a SignInService that I am trying inject in the different pages using get_it service locator.

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;
.
.
}

When I log in, firebaseUser variable is getting set in this class and the value is not null.

When I use it in another page like so, I get a null.

E/flutter (20069): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null. E/flutter (20069): Receiver: null E/flutter (20069): Tried calling: 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(
        //...
     );
  }


Solution 1: Fred Grott

It's not null if you do GetIt.find(YourName); as the first line