0

I'm new ChangeNotifier provider flutter. I'm trying to get the user id from firebase. I want to query the data of current user from firebase. But when I run my app, the error happens.

NoSuchMethodError (NoSuchMethodError: The setter 'uid=' was called on null.
Receiver: null
Tried calling: uid="AFdLhdPxWmWZ5D1HjttjQvrvKX42")

I don't know what wrong with my code and how to solve this problem.

This is code that I get problem.

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final Completer<GoogleMapController> _mapController = Completer();
  Customer _currentCustomer;

  @override
  void initState() {
    AuthNotifier authNotifier =
        Provider.of<AuthNotifier>(context, listen: false);
    if (authNotifier.currentCustomer != null) {
      _currentCustomer = authNotifier.currentCustomer;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    AuthNotifier authNotifier = Provider.of<AuthNotifier>(context);

    return Scaffold(
        appBar: AppBar(
          title: Text(_currentCustomer.uid),
        ),
        body: Container(
            child: Column(children: [
          Flexible(flex: 2, child: MapWidget(mapController: _mapController)),
          SizedBox(height: 12),
          Flexible(
              flex: 2, child: StoreListWidget(mapController: _mapController)),
          SizedBox(height: 12),
          FloatingActionButton.extended(
            onPressed: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) =>
                          SelectMap(uid: _currentCustomer.uid)));
            },
            label: Text('Get Current Location'),
            icon: Icon(Icons.directions_boat),
          )
        ])));
  }
}

This is ChangeNotifier class.

class AuthNotifier with ChangeNotifier {
  User _user;
  Customer _currentCustomer;

  User get user => _user;
  Customer get currentCustomer => _currentCustomer;

  void setUser(User user) {
    _user = user;
    notifyListeners();
  }

  void setCurrentCustomer(String uid) {
    _currentCustomer.uid = uid;
    notifyListeners();
  }
}

This is authentication code.

login(Customer customer, AuthNotifier authNotifier) async {
  BuildContext context;
  UserCredential authResult = await FirebaseAuth.instance
      .signInWithEmailAndPassword(
          email: customer.email, password: customer.password)
      .catchError((error) => print(error));

  if (authResult != null) {
    User firebaseUser = authResult.user;

    if (firebaseUser != null) {
      print('Login with: $firebaseUser');
      authNotifier.setUser(firebaseUser);
      authNotifier.setCurrentCustomer(firebaseUser.uid);
    }
  }
}

register(Customer customer, AuthNotifier authNotifier) async {
  UserCredential authResult = await FirebaseAuth.instance
      .createUserWithEmailAndPassword(
          email: customer.email, password: customer.password)
      .catchError((error) => print(error));

  if (authResult != null) {
    User firebaseUser = authResult.user;

    if (firebaseUser != null) {
      BuildContext context;
      FirebaseFirestore.instance.collection('users').doc(firebaseUser.uid).set({
        'uid': firebaseUser.uid,
        'userName': customer.userName,
        'email': customer.email,
      })
      .catchError((error) => print("Failed to add user: $error"));
      User currentUser = await FirebaseAuth.instance.currentUser;
      authNotifier.setUser(currentUser);
      authNotifier.setCurrentCustomer(firebaseUser.uid);
    }
  }
}

initializeCurrentUser(AuthNotifier authNotifier) async {
  User firebaseUser = await FirebaseAuth.instance.currentUser;

  if (firebaseUser != null) {
    print(firebaseUser);
    authNotifier.setUser(firebaseUser);
    authNotifier.setCurrentCustomer(firebaseUser.uid);
  }
}
eight
  • 79
  • 1
  • 6

1 Answers1

0

In your AuthNotifier class, you don't seem to have initialised _currentCustomer.

class AuthNotifier with ChangeNotifier {
    User _user;
    Customer _currentCustomer; // <---- No where initialised

    User get user => _user;
    Customer get currentCustomer => _currentCustomer;

    void setUser(User user) {
      _user = user;
      notifyListeners();
    }

    void setCurrentCustomer(String uid) {
      _currentCustomer.uid = uid;
      notifyListeners();
    }
}

So in _currentCustomer.uid = uid;, _currentCustomer will be null.

I also suggest that you migrate your code to Sound Null Safety if possible. It helps you avoid these issues at compile time.

Nisanth Reddy
  • 5,967
  • 1
  • 11
  • 29