简体   繁体   English

Flutter firebase 供应商架构

[英]Flutter firebase provider architecture

I have a stateful widget that has an animated container.我有一个有状态的小部件,它有一个动画容器。 Inside that animated container I have a streamProvider connected to firebase.在那个动画容器中,我有一个连接到 firebase 的 streamProvider。 My problem is that when I animate using setState the entire widget rebuilds and another call to firebase is made.我的问题是,当我使用 setState 制作动画时,整个小部件会重建,并再次调用 firebase。 My solution was to lift the streamProvider up and wrap the widget that's animated with that streambuilder.我的解决方案是提升 streamProvider 并包装使用该流构建器进行动画处理的小部件。 But that means I need to create another widget and hence more boilerplate.但这意味着我需要创建另一个小部件,因此需要更多样板。

I feel like what I'm doing is wrong but I'm kind of stuck because all provider resources are related to authentication...我觉得我正在做的事情是错误的,但我有点卡住了,因为所有提供者资源都与身份验证有关......

Does anyone have any ideas how I can get around this in a clean way?有谁知道如何以干净的方式解决这个问题? and is setState the right way to trigger animations in a stateful widget? setState 是在有状态小部件中触发动画的正确方法吗?

For animating, try using AnimatedBuilder its the easiest way to animate, but I guess it won't fix your issue.对于动画,尝试使用AnimatedBuilder最简单的动画方法,但我想它不会解决您的问题。

Personally I always use the Provider package, I don't know if you are doing it too.就个人而言,我一直使用 Provider package,不知道你是不是也在这样做。

So usually firebase provides you with a stream of data (if you are using it with cloud functions its different)所以通常firebase为您提供stream的数据(如果您将它与云功能一起使用则不同)

Now you could use a StreamBuilder with the Stream firebase provides you and use the data of the stream.现在您可以使用带有StreamBuilder firebase 的 StreamBuilder 为您提供并使用 stream 的数据。 With this version rebuilding the Widget won't lead to the app connecting to the server and fetching new data.在此版本中,重建 Widget 不会导致应用程序连接到服务器并获取新数据。

If you really like to use a ChangeNotifier you can use that stream inside the ChangeNotifier , listen to it and always notifying listeners of changes to occur with this implementation there won't be any unnecessary network calls either.如果您真的喜欢使用ChangeNotifier ,您可以在ChangeNotifier中使用 stream ,听它并始终通知侦听器此实现发生的更改,也不会有任何不必要的网络调用。

Some examples for the second version:第二个版本的一些例子:

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

I hope this answers your question.我希望这回答了你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM