34

So in my app, I want to make an Ajax request as soon as the widget is mounted, not in initState(). Similar to ComponentWillMount() in react

1
  • 3
    This and this might help. Commented Feb 3, 2019 at 9:40

7 Answers 7

53

if the Widget has not mounted then return. Do it before the setState method

if (!mounted) return;
setState(() {});

or

if (mounted) {
   //Do something
};
setState(() {});

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

1 Comment

"It is an error to call setState unless mounted is true." - official docs .. api.flutter.dev/flutter/widgets/State/mounted.html
23

If you want to execute some code as soon as the widget loaded , you could simply put this code in the initstate like the following ;

 void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) => yourFunction(context));   
 }

In this way , the yourFunction will be exectued as soon as the first frame of the widget loaded on the screen.

2 Comments

this should be marked an answer
@AkbarNoto I think you mean, THE answer 🤓
12

With the latest update, Flutter 3.7, you can directly check whether the widget is mounted using the context. Here's an example use:

@override
  Widget build(BuildContext context) {
    return OutlinedButton(
      onPressed: () async {
        await Future<void>.delayed(const Duration(seconds: 1));
        if (context.mounted) {
          Navigator.of(context).pop();
        }
      },
      child: const Text('Delayed pop'),
    );
  }

For details, refer to this page.

Comments

8

I don't think it's currently possible.

Here's the mounted property: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/framework.dart#L974

bool get mounted => _element != null;

And here's when _element is set: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/framework.dart#L3816

_state._element = this

And I don't see any hook around this code that would inform us.

Why not use initState anyway? It's probably what you want. Here's the comment above the mounted property: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/framework.dart#L967

  /// After creating a [State] object and before calling [initState], the
  /// framework "mounts" the [State] object by associating it with a
  /// [BuildContext]. The [State] object remains mounted until the framework

Comments

8

Simply do this as follows.

if (this.mounted) {
        setState(() {
         //Your code
        });
}

Comments

1

I know this answer comes a little bit late but...

Inside your method, you should have something like this:

if(mounted){
   setState(() {});
 }

This would help, to rebuild the UI only if something changes. I use this myself inside a method, where I fill my list with users from firestore.

The mounted property helps to avoid the error, when you trying to call setState before build.

1 Comment

you don't need a boolean operator to check a boolean value.
1

For setState, you can override it with mounted check, so you don't need to use the check all the time.

  @override
  void setState(fn) {
    if (mounted) {
      super.setState(fn);
    }
  }

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.