[My code] . When I use User user = Provider.of(context, listen: false).user; => user null My code here
class UserController extends ChangeNotifier {
User _user;
User get user => _user;
Future<void> getUser() async {
String token = await AppValue.getToken();
Uri uri = Uri.parse('http://20.89.111.129/api/user/getProfileUser');
Map<String, String> headers = {'Authorization': 'Bearer $token'};
try {
final response = await http.get(
uri,
headers: headers,
);
final data = jsonDecode(response.body);
User newUser = new User(
address: (data['address'] == null) ? '' : data['address'],
birthday: (data['birthday'] == null) ? '' : data['birthday'],
email: (data['email'] == null) ? '' : data['email'],
fullName: (data['fullName'] == null) ? '' : data['fullName'],
);
_user = newUser; // _user is not null
notifyListeners();
} catch (error) {
log('ST WRONG!');
throw (error);
}
}
}
// When I use User user = Provider.of(context, listen: false).user; => user null
CodePudding user response:
I think you need to tell the provider the exact class you want to act on,
//so instead of this;
User user = Provider.of(context, listen: false).user;
// do this
User user = Provider.of< UserController>(context, listen: false).user;