怎么从类扩展了ChangeNotifier获得价值



[我的代码]。当我使用User User = Provider时。Of (context, listen: false).user;=比;用户为空我的代码在这里

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

//当我使用User User = Provider。Of (context, listen: false).user;=比;用户零

我认为你需要告诉提供商你想要操作的确切类,

//so instead of this;
User user = Provider.of(context, listen: false).user;
// do this
User user = Provider.of< UserController>(context, listen: false).user;