Flutter Provider 不重建小部件
Flutter Provider doesn't rebuild widget
这是我的一段代码,
但请在下结论之前阅读问题:
class RescheduleCard extends StatelessWidget {
@override
Widget build(BuildContext context)=> Consumer<AppSnapshot>(
builder: (context, appSnapshot, _)=>
(appSnapshot.reschedule!=null)
?RescheduleBuilder(
model: appSnapshot.reschedule,
controllers: appSnapshot.controllers,
)
:Carouselcard(
title: carouselPageTitle(title: rescheduleTitle,test: appSnapshot.test),
colors: yellows,
所以我对提供者有点陌生,而且我已经习惯了 Bloc,
我的 api 一旦接到供应商的电话并设置模型;
编辑:这里是提供者 256 行被绊倒的问题
"reschedule"...
class AppSnapshot with ChangeNotifier {
RescheduleModel _reschedule;
RescheduleModel get reschedule => _reschedule;
void cleanReschedule() {
reschedule=null;
notifyListeners();
}
set reschedule(RescheduleModel reschedule) {
_reschedule=reschedule;
notifyListeners();
}
}
重新编辑:最重要的是:
void main() async {
final AppSnapshot _appSnapshot = AppSnapshot();
await _appSnapshot.load();
runApp(ChangeNotifierProvider(
builder: (context) => _appSnapshot,
child: Initializer()));
}
我期待 "Consumer" 重建我的小部件,
但没有!
我确定数据在那里,因为小部件在轮播中
一旦我移动它,它就会正确重建。
我错过了什么?
谢谢
re-re-edit:这是另一个 Carousel Card 的示例,其中提供者工作迅速并且实时应用更改
Consumer<AppSnapshot>(
builder: (context, appSnapshot, _)=> Carouselcard(
title: carouselPageTitle(title: optionsTitle,test: appSnapshot.test),
colors: lightBlues,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
((appSnapshot.id??'')!='') // ID WIDGET
? ListTile(
leading: CustomIcon(
icon: deleteIcon,
onTap: () => appSnapshot.deleteID(),
),
title: _customCard(onTap:()=> _showID(id: appSnapshot.id,context: context)),
subtitle: Text(tap2delete,textScaleFactor:0.8),
)
: IdTile(),
指定 main.dart 中的类型
void main() async {
final AppSnapshot _appSnapshot = AppSnapshot();
await _appSnapshot.load();
runApp(ChangeNotifierProvider< **AppSnapshot** >(
builder: (context) => _appSnapshot,
child: Initializer()));
}
根据这个 article FutureProvider
不会监听变化,它只会重建 Consumers
1 次,当 Future 完成时。
文章甚至解释了作者是如何测试的。
Dart Provider 包的开发者也 用于 FutureProvider
。
这是我的一段代码, 但请在下结论之前阅读问题:
class RescheduleCard extends StatelessWidget {
@override
Widget build(BuildContext context)=> Consumer<AppSnapshot>(
builder: (context, appSnapshot, _)=>
(appSnapshot.reschedule!=null)
?RescheduleBuilder(
model: appSnapshot.reschedule,
controllers: appSnapshot.controllers,
)
:Carouselcard(
title: carouselPageTitle(title: rescheduleTitle,test: appSnapshot.test),
colors: yellows,
所以我对提供者有点陌生,而且我已经习惯了 Bloc, 我的 api 一旦接到供应商的电话并设置模型;
编辑:这里是提供者 256 行被绊倒的问题 "reschedule"...
class AppSnapshot with ChangeNotifier {
RescheduleModel _reschedule;
RescheduleModel get reschedule => _reschedule;
void cleanReschedule() {
reschedule=null;
notifyListeners();
}
set reschedule(RescheduleModel reschedule) {
_reschedule=reschedule;
notifyListeners();
}
}
重新编辑:最重要的是:
void main() async {
final AppSnapshot _appSnapshot = AppSnapshot();
await _appSnapshot.load();
runApp(ChangeNotifierProvider(
builder: (context) => _appSnapshot,
child: Initializer()));
}
我期待 "Consumer" 重建我的小部件, 但没有!
我确定数据在那里,因为小部件在轮播中 一旦我移动它,它就会正确重建。
我错过了什么? 谢谢
re-re-edit:这是另一个 Carousel Card 的示例,其中提供者工作迅速并且实时应用更改
Consumer<AppSnapshot>(
builder: (context, appSnapshot, _)=> Carouselcard(
title: carouselPageTitle(title: optionsTitle,test: appSnapshot.test),
colors: lightBlues,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
((appSnapshot.id??'')!='') // ID WIDGET
? ListTile(
leading: CustomIcon(
icon: deleteIcon,
onTap: () => appSnapshot.deleteID(),
),
title: _customCard(onTap:()=> _showID(id: appSnapshot.id,context: context)),
subtitle: Text(tap2delete,textScaleFactor:0.8),
)
: IdTile(),
指定 main.dart 中的类型
void main() async {
final AppSnapshot _appSnapshot = AppSnapshot();
await _appSnapshot.load();
runApp(ChangeNotifierProvider< **AppSnapshot** >(
builder: (context) => _appSnapshot,
child: Initializer()));
}
根据这个 article FutureProvider
不会监听变化,它只会重建 Consumers
1 次,当 Future 完成时。
文章甚至解释了作者是如何测试的。
Dart Provider 包的开发者也 FutureProvider
。