Flutter Hooks – useEffect, useState

Table of Contents

useEffect :

We have carried out useEffect employing flutter hooks and tried using to change the use of init state and dispose in this component of the tutorial these two elements are part  of lifecycle of widget.

In this element of the tutorial we will check out to update the counter mechanically with the enable of Timer wherever we will replace the initialization and dispose stage of the controllers with hooks.

useEffect is referred to as on every build phone i..e, only as soon as and if there are any keys specified in the constructor which are up-to-date then the useEffect can be identified as many periods centered on changes.

This element accepts a operate and returns a functionality ordinarily subscribes to a stream and disposes it once completed.

Go by the past website for simple linked to flutter hooks specific explanation with online video tutorial.

 

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

 

pubspec.yaml:

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

dependencies:
  flutter:
    sdk: flutter

  flutter_hooks: ^.18.3

 

 

key.dart :

In this case in point we will consider to skip init and dispose blocks and useEffect to initialize and dispose the timer as soon as its applied.This will be considerably successful in memory leaks managing.

class _HomeState extends State {

  late Timer _timer
  int _count = 

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

  @override
  Widget develop(BuildContext context) 
    return Middle(kid: Text("$_count",style: TextStyle(fontSize: 50.),))
  

  @override
  void dispose() 
    _timer.cancel()
    tremendous.dispose()
  
}

class HooK extends HookWidget {
   HooK(Essential? essential) : tremendous(vital: crucial)

  @override
  Widget develop(BuildContext context) 

    closing _rely = useState()

    useEffect(()
      closing timer = Timer.periodic(Period(seconds: 1), (timer) 
        _count.worth = timer.tick
      )
      return timer.terminate
    ,[])

    return Middle(baby: Textual content("$_rely.benefit",style: TextStyle(fontSize: 50.),))
  
}

 

 

If you have any query’s in this section of the tutorial do enable us know in the comment portion beneath.

Next Post

Laugh Your Way to Good Health: The Many Health Benefits of Laughter

July 1st is International Joke Day. That has influenced me to emphasis on laughter for the entire month of July.  Chortle Your Way to Superior Well being: The Many Well being Advantages of Laughter Most people enjoys a very good giggle, no matter if it’s from a sitcom on Television […]
Laugh Your Way to Good Health: The Many Health Benefits of Laughter

You May Like