2

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}');
    notifyListeners();
  }

  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;
      notifyListeners();
      return true;
    }
    notifyListeners();
    print(_token);
    return true;
  }
}

2 Answers 2

1

You just have to call "notifyListeners();" at the end of the method

3
  • Putting notifier listener at the end is not working Jan 5, 2021 at 11:50
  • What are you trying to do exactly ? Maybe you didn't decode all the model which returns from api like your second method that's why ? @ShahryarRafique Jan 7, 2021 at 18:19
  • Thanks for the answer. The issue is because I am accessing it like Auth().authenticate() but have to use it like Provider.of<Auth>(context).authenticate(); Jan 7, 2021 at 18:27
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.