Flutter Hooks – useEffect, useState

useEffect :

We have executed useEffect making use of flutter hooks and tried to swap the usage of init condition and dispose in this aspect of the tutorial these two parts are part  of lifecycle of widget.

In this element of the tutorial we will attempt to update the counter quickly with the assist of Timer exactly where we will exchange the initialization and dispose stage of the controllers with hooks.

useEffect is referred to as on each individual construct get in touch with i..e, only when and if there are any keys specified in the constructor which are up to date then the useEffect can be named a number of times based mostly on variations.

This component accepts a perform and returns a operate generally subscribes to a stream and disposes it after completed.

Go through the prior blog for standard associated to flutter hooks specific explanation with video clip tutorial.


https://www.youtube.com/check out?v=r-boA_drJLs



Insert flutter hooks dependency to make use of useEffect and useState i.e., hooks ideas in your flutter application.

    sdk: flutter

  flutter_hooks: ^.18.3



main.dart :

In this example we will try out to skip init and dispose blocks and useEffect to initialize and dispose the timer as soon as its employed.This will be a great deal efficient in memory leaks managing.

class _HomeState extends State {

  late Timer _timer
  int _depend = 

  void initState() 
    _timer = Timer.periodic(Period(seconds: 1), (abc) 
        _rely = abc.tick

  Widget establish(BuildContext context) 
    return Middle(youngster: Text("$_count",fashion: TextStyle(fontSize: 50.),))

  void dispose() 

course HooK extends HookWidget {
   HooK(Essential? critical) : super(critical: critical)

  Widget make(BuildContext context) 

    ultimate _depend = useState()

      remaining timer = Timer.periodic(Duration(seconds: 1), (timer) 
        _rely.worth = timer.tick
      return timer.terminate

    return Centre(little one: Textual content("$_rely.benefit",design: TextStyle(fontSize: 50.),))



If you have any query’s in this element of the tutorial do permit us know in the remark portion beneath.