如何在 flutter with provider 中使用 changeNotifier

How to use changeNotifier in flutter with provider

我正在为我的项目使用提供程序包。我使用 ChangeNotifier 在我的 Auth 模型中创建了两个函数。第一个是身份验证,第二个是 tryAutoLogin。如果我调用 tryAutoLogin 函数,那么 getter 得到的值就像 userCode 得到我在函数中分配给代码变量的值,就像其他 getter 一样。但是 authenticate 没有得到值。你能告诉我我做错了什么吗?

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

您只需调用“notifyListeners();”在方法的末尾

问题是因为我像 Auth().authenticate() 一样访问它,但必须像 Provider.of<Auth>(context).authenticate();

一样使用它