我的 changenotifierprovider 没有更新。不知道为什么
My changenotifierprovider is not updating. Not sure why
这是我的更改通知程序 class。
class UserChamaNotifier with ChangeNotifier {
final List<UserChama> _userChamaList = [];
UnmodifiableListView<UserChama> get userchamaListy =>
UnmodifiableListView(_userChamaList);
void addUserChama(UserChama userchama) {
_userChamaList.add(userchama);
notifyListeners();
}
}
我已经在 main.dart 中创建了提供程序:
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => _appStateManger,
),
ChangeNotifierProvider(
create: (context) => _profileManager,
),
ChangeNotifierProvider(
create: (context) => UserChamaNotifier(),
)
],
然后我继续将 chama 对象添加到我的列表中:
UserChama userChama =
UserChama(id: s['Id'], phone: s['Phone'], name: s['Name']);
print(userChama.phone);
Provider.of<UserChamaNotifier>(context).addUserChama(userChama);
这里我尝试通过提供商访问列表:
class ChamaList extends StatelessWidget {
const ChamaList({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
UserChamaNotifier userChamaNotifier =
Provider.of<UserChamaNotifier>(context, listen: true);
return Text(userChamaNotifier.userchamaListy.length.toString());
}
}
至此,我尝试了很多,还是没有找到正确的实现方式。
添加数据时,设置listen:false
Provider.of<UserChamaNotifier>(context,listen:false)
.addUserChama(userChama);
查看更多.
这是我的更改通知程序 class。
class UserChamaNotifier with ChangeNotifier {
final List<UserChama> _userChamaList = [];
UnmodifiableListView<UserChama> get userchamaListy =>
UnmodifiableListView(_userChamaList);
void addUserChama(UserChama userchama) {
_userChamaList.add(userchama);
notifyListeners();
}
}
我已经在 main.dart 中创建了提供程序:
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => _appStateManger,
),
ChangeNotifierProvider(
create: (context) => _profileManager,
),
ChangeNotifierProvider(
create: (context) => UserChamaNotifier(),
)
],
然后我继续将 chama 对象添加到我的列表中:
UserChama userChama =
UserChama(id: s['Id'], phone: s['Phone'], name: s['Name']);
print(userChama.phone);
Provider.of<UserChamaNotifier>(context).addUserChama(userChama);
这里我尝试通过提供商访问列表:
class ChamaList extends StatelessWidget {
const ChamaList({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
UserChamaNotifier userChamaNotifier =
Provider.of<UserChamaNotifier>(context, listen: true);
return Text(userChamaNotifier.userchamaListy.length.toString());
}
}
至此,我尝试了很多,还是没有找到正确的实现方式。
添加数据时,设置listen:false
Provider.of<UserChamaNotifier>(context,listen:false)
.addUserChama(userChama);
查看更多