【问题标题】:How to use changeNotifier in flutter with provider如何在 Flutter 中使用 changeNotifier 与提供者
【发布时间】:2021-04-10 23:45:26
【问题描述】:

我正在为我的项目使用提供程序包。我使用 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;
  }
}

【问题讨论】:

    标签: flutter dart getter provider flutter-change-notifier


    【解决方案1】:

    问题是因为我像 Auth().authenticate() 一样访问它,但必须像 Provider.of&lt;Auth&gt;(context).authenticate(); 一样使用它

    【讨论】:

      【解决方案2】:

      你只需要调用“notifyListeners();”在方法结束时

      【讨论】:

      • 把通知监听器放在最后是行不通的
      • 你到底想做什么?也许您没有像您的第二种方法那样解码从 api 返回的所有模型,这就是为什么? @ShahryarRafique
      • 感谢您的回答。问题是因为我像 Auth().authenticate() 一样访问它,但必须像 Provider.of(context).authenticate(); 一样使用它
      猜你喜欢
      • 2020-10-16
      • 2020-01-06
      • 2023-03-29
      • 2021-05-18
      • 2020-09-01
      • 1970-01-01
      • 2021-03-28
      • 2021-09-30
      • 2020-01-07
      相关资源
      最近更新 更多