la-haus / flutter-segment

Segment.io library for flutter
MIT License
22 stars 139 forks source link

Why is the Segment.screen() method called in the build() method? #25

Closed ArmandoJimenezOS closed 2 years ago

ArmandoJimenezOS commented 2 years ago

Hi,

As I understand, the build method can be called multiple times per second, and this track screen function is a method channel. Was this intention? or what's the best practice to log a screen without it being called multiple times in the same screen session?

danielgomezrico commented 2 years ago

@ArmandoJimenezOS thanks for reporting it, can you show the line or file where you see this?

ArmandoJimenezOS commented 2 years ago

Thanks for answering. In the examples of this package, it shows calling Segment.screen() in the build method, is this where we are supposed to call it?

ariefwijaya commented 2 years ago

Thanks for answering. In the examples of this package, it shows calling Segment.screen() in the build method, is this where we are supposed to call it?

No, it depend on your case. In the examples, it is a StatelessWidget, so the build method will never called multiple time unless you reopen the widget.

The conclusion :

  1. In the StatelessWidget, you can call Segment.screen() in the build method
  2. In the StatefulWidget, you must call Segment.screen() in the initState method.