对于动画,尝试使用AnimatedBuilder 这是最简单的动画方法,但我想它不会解决您的问题。
我个人一直都是用Provider包,不知道你是不是也在用。
所以通常firebase会为您提供数据流(如果您将它与云功能一起使用,则不同)
现在您可以使用 StreamBuilder 与 Stream firebase 为您提供并使用流的数据。在此版本中,重建 Widget 不会导致应用连接到服务器并获取新数据。
如果您真的喜欢使用ChangeNotifier,您可以在ChangeNotifier 中使用该流,收听它并始终通知侦听器此实现发生的更改,也不会有任何不必要的网络调用。
第二版的一些例子:
class SomeNotifier extends ChangeNotifier {
List<MyData> dataList = [];
SomeNotifier() {
Firestore.instance.collection("MyCollection").snapshots().listen((data) {
dataList = data.documents.map((doc) => MyData.fromDoc(doc));
notifyListeners();
});
}
}
class _MyWidgetState extends State<MyWidget>
with SingleTickerProviderStateMixin {
AnimationController _controller;
@override
void initState() {
_controller = AnimationController(vsync: this);
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<SomeNotifier>(
create: (context) => SomeNotifier(),
child: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
var notifier = Provider.of<SomeNotifier>(context);
return Container(); //Here you can use your animated widget, it will be rebuilt to animate propperly
//It will also rebuild every time data in firebase changes
},
),
);
}
}
我希望这能回答你的问题。