How to use changeNotifier in flutter with provider


I am using a provider package for my project. I made two functions in my Auth model with ChangeNotifier. The first one is authenticate and the second one is tryAutoLogin. If I call the tryAutoLogin function then the getter gets the value of like userCode get the value which i assign in the function to code variable and just like that other getters. but authenticate did not get the value . Can you tell me what i am doing wrong.

import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:neighbourhood_watch/models/http_Exception.dart';
import 'package:neighbourhood_watch/models/user.dart';
import 'package:neighbourhood_watch/api.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Auth with ChangeNotifier {
  int _code;
  List<UserElement> _user;
  String _message;
  String _token;
  bool get isAuth {
    return token != null;

  int get userCode {
    if (_code != null) return _code;
    return null;

  String get userApiMessage {
    if (_message != null) return _message;
    return null;

  UserElement get apiUser {
    if (_user != null) {
      print('This is the Getter of the User ${_user.first.token}');
      return _user.first;
    return null;

  String get token {
    if (_token != null) {
      return _token;
    return null;

  Future<void> authenticate(String email, String password) async {
    User responseData = await UserApi().apiLoginPost(email, password);
    if (responseData.code != 200) {
      _code = responseData.code;
      _message = responseData.message;
      throw HttpException(responseData.message);

    SharedPreferences pref = await SharedPreferences.getInstance();
    final userData = json.encode({
      'email': email,
      'password': password,
    //Save the User Inforamtion in the Shared Preferences
    pref.setString('userData', userData);
    print('This is the first reponse Data ${responseData.user[0].token}');
    _code = responseData.code;
    print('Response Code $_code}');

    _message = responseData.message;
    print('Response Message $_message');
    _user = responseData.user;
    print('Response User ${_user.first.token}');
    print('This call from function authehicate ${_user.first.token}');

  Future<bool> tryAutoLogIn() async {
    final prefs = await SharedPreferences.getInstance();
    final extractedUserData =
        json.decode(prefs.getString('userData')) as Map<String, Object>;
    if (!prefs.containsKey('userData')) {
      return false;

    if (prefs.containsKey('userData')) {
      print('This is the user Token ${extractedUserData['email']}');
      print('This is the fcm Token ${extractedUserData['password']}');
      User responsData2 = await UserApi().apiLoginPost(
          extractedUserData['email'], extractedUserData['password']);
      _token = extractedUserData['email'];
      _code = responsData2.code;
      _message = responsData2.message;
      _user = responsData2.user;
      return true;
    return true;


The issue is because I am accessing it like Auth().authenticate() but have to use it like Provider.of<Auth>(context).authenticate();

Answered By – Shahryar Rafique

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *