如何从工厂制作 Flutter 提供者 notifyListeners()?
How to make Flutter provider notifyListeners() from a factory?
我有一个 Auth 提供者 class。当应用程序加载时,我使用工厂方法 (Auth.fromJson) 将数据映射到 Auth class 中,调用 API 其中 returns json。映射完成后,我希望通知听众以便更新相关的 UI。所以事实证明我无法从工厂构造函数调用 notifyListeners() 因为我收到此错误:
instance members cannot be accessed from a factory constructor
为什么会这样?我可以实施什么解决方法?在工厂映射数据后,我需要能够以某种方式通知监听器。
class Auth with ChangeNotifier {
String token;
String organisationId;
String domain;
String userId;
Auth(
{this.token,
this.organisationId,
this.domain,
this.userId});
factory Auth.fromJson(Map<String, dynamic> json) {
Auth(
token: json['token'],
organisationId: json['organisationId'],
domain: json['domain'],
userId: json['userId'],
);
notifyListeners(); // Error here.
return Auth();
}
}
- 工厂方法很像静态方法。您无法访问 class 变量和方法的方式也适用于工厂。
- notifyListeners();是 ChangeNotifier class 的一种方法,因此您无法通过任何静态方法或工厂方法访问它。
- 您将需要一个 Auth 实例来调用 notifyListeners();
- 如果您真的想观察 Auth 中的更改,最好不要将 Auth 设置为 ChangeNotifier,然后创建一个包含 Auth 值的 ChangeNotifer。以下是相关代码。
import 'package:flutter/material.dart';
class Auth{
String token;
String organisationId;
String domain;
String userId;
Auth(
{this.token,
this.organisationId,
this.domain,
this.userId});
factory Auth.fromJson(Map<String, dynamic> json) {
return Auth(
token: json['token'],
organisationId: json['organisationId'],
domain: json['domain'],
userId: json['userId'],
);
}
}
class AuthChangeNotifier with ChangeNotifier {
Auth auth;
onNewAuth(Auth newAuth){
this.auth = newAuth;
notifyListeners();
}
}
- 您也可以对这个用例使用
ValueNotifier<Auth>
并使用 ValueListenableBuilder<Auth>
观察它
希望对您有所帮助,如有任何疑问,请告诉我。
我有一个 Auth 提供者 class。当应用程序加载时,我使用工厂方法 (Auth.fromJson) 将数据映射到 Auth class 中,调用 API 其中 returns json。映射完成后,我希望通知听众以便更新相关的 UI。所以事实证明我无法从工厂构造函数调用 notifyListeners() 因为我收到此错误:
instance members cannot be accessed from a factory constructor
为什么会这样?我可以实施什么解决方法?在工厂映射数据后,我需要能够以某种方式通知监听器。
class Auth with ChangeNotifier {
String token;
String organisationId;
String domain;
String userId;
Auth(
{this.token,
this.organisationId,
this.domain,
this.userId});
factory Auth.fromJson(Map<String, dynamic> json) {
Auth(
token: json['token'],
organisationId: json['organisationId'],
domain: json['domain'],
userId: json['userId'],
);
notifyListeners(); // Error here.
return Auth();
}
}
- 工厂方法很像静态方法。您无法访问 class 变量和方法的方式也适用于工厂。
- notifyListeners();是 ChangeNotifier class 的一种方法,因此您无法通过任何静态方法或工厂方法访问它。
- 您将需要一个 Auth 实例来调用 notifyListeners();
- 如果您真的想观察 Auth 中的更改,最好不要将 Auth 设置为 ChangeNotifier,然后创建一个包含 Auth 值的 ChangeNotifer。以下是相关代码。
import 'package:flutter/material.dart';
class Auth{
String token;
String organisationId;
String domain;
String userId;
Auth(
{this.token,
this.organisationId,
this.domain,
this.userId});
factory Auth.fromJson(Map<String, dynamic> json) {
return Auth(
token: json['token'],
organisationId: json['organisationId'],
domain: json['domain'],
userId: json['userId'],
);
}
}
class AuthChangeNotifier with ChangeNotifier {
Auth auth;
onNewAuth(Auth newAuth){
this.auth = newAuth;
notifyListeners();
}
}
- 您也可以对这个用例使用
ValueNotifier<Auth>
并使用ValueListenableBuilder<Auth>
观察它
希望对您有所帮助,如有任何疑问,请告诉我。