Reputation: 788
I have a Provider of an Auth class. When the app loads, I call an API which returns json with data which I map into the Auth class using a factory method (Auth.fromJson). Once the mapping completes, I wish to notify listeners so that the relevant UI is updated. So it turns I cannot call notifyListeners() from the factory constructor because I get this error:
instance members cannot be accessed from a factory constructor
Why is this so? What workaround can I implement? After the factory maps data I need to be able to notifyListeners somehow.
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();
}
}
Upvotes: 1
Views: 3801
Reputation: 5763
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>
for this use-case and observe it using ValueListenableBuilder<Auth>
Hope it helps, let me know if you have any doubts.
Upvotes: 4