0

I have this function in a widget (homescreen):

void toggleRecording() async {

  // HERE IS THE CONFUSION I GUESS
  _isRecording = !_isRecording;
  recorder = SoundStream(isRecording: _isRecording);

  //recorder.toggleRecording(_isRecording);
  setState(() {
    _isRecording = recorder.isRecording;
  });
  if (_isRecording) {
    startTimer();
    _stopwatch.start();
  } else {
    stopTimer();
    _stopwatch.stop();
  }
}

It needs to call (trigger) another function in my recorder class:

void toggleRecording() async {

  widget.isRecording  ////// currently being passed as an argument from homescreen

      ? {_recorder.stop, await save(_micChunks, 44100)}
      : _recorder.start;
}

Also, the boolean variable _isRecording is present in both the classes. How do I sync the state?

3
  • This is a state management problem Try out provider or riverpod for starters, in which you can expose some state across multiple widgets. Commented Oct 19, 2021 at 21:10
  • I tried provider. Couldn't get everything working together. Commented Oct 20, 2021 at 6:37
  • Provider indeed is easy at the beginning, but it's kind-of hard to get everything to work altogether at some point. Try out Riverpod, that's the production-ready version of Provider in my opinion. Version 1.0 is coming soon~ish, too. Commented Oct 21, 2021 at 7:42

2 Answers 2

2

In your situation passing reference of function through widgets will work. However best practice of this will be using provider package.

Managing functions from provider will allow you to control functions from all pages.

If you change a variable you can call notifylistener() function inside your provider function. So that you can change state of widget.

I will try to explain it in a glance however this is an important subject of flutter.

Here is my folder structure

At provider folder we define our provider classes.

Firstly i define class which extends changeNotifier class. This is what make this class provider.

Side note: notifyListener() function here calls setState of every widget if you use any variables inside that class and this is what you are searching for.

Then i import it into my main.dart file or whatever file you want. Only condition is being above the widget that you will use provider at.

At last you can use your function at everywhere if you import provider package and define your provider like i did in this code.

At last here is the visualized stucture of provider package.

I wish i explained it well. There is more about it on youtube.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please show some code example?
0
Pass the function to other widget 
using Function keyword


Say ContentWidget is your child and ParentWidget is parent

class ParentWidget extends StatefulWidget {

//Do Something
void onSomeFunction()
{
ContentWidget(onTimerUpdate:onTimerClosed)

}

void onTimerClosed()
{
//Perform Operation on Timer Change
}



}

class ContentWidget extends StatefulWidget {
 final Function onTimerUpdate;

ContentWidget({
 Key key,
 @required this.onTimerUpdate,
  }) : super(key: key);

void onAnyActionFromChild()
{
widget.onTimerUpdate() //Note () can have any param or can be null
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.