FutureBuilder method inside future called twice

This issue has been tracked since 2022-09-22.

I am using the FutureBuilder widget as per documentation as follows:

late Future _initWidget;

@override
  void initState() {
    _initWidget =  _initializeWidget();
    super.initState();
  }

@override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initWidget,
      builder: (BuildContext context, AsyncSnapshot snapshot){
          //Not important stuff
      }
    );
  }

_initializeWidget() async{
    print("Initializing");
    if(user.isEmpty()){
      await GetUserInfo().getUserData();
    }
    _plans = await GetUserInfo().getPlans();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async{
      print("Setting callback");
      if(widget.args.initialPage == 0 && _plans.length > 1)
        _timer = Timer.periodic(Duration(seconds: 3), (timer) => _pageController.animateTo(20, duration: Duration(milliseconds: 400), curve: Curves.easeOut));

      if(widget.args.subscriptionProcessInitiated && user.paymentMethods.isNotEmpty){
        _btnController.start();
        _payAndSubscribe();
      }
    });
  }

  Future<void> _payAndSubscribe() async{
    print("Paying");
    int index = _pageController.page!.round();
    bool? success = await startSubscription(context, index, _plans[index]['id']);

    if(success == null){
      _btnController.reset();
      return;
    }

    if(success){
      _btnController.success();

      openCustomerPortal(context, NetworkService().getSiteUrl() + '/accountPage');
      Navigator.pushReplacementNamed(context, '/accountPage');
    }else{
      _btnController.error();
      setState(() {});
    }
  }

And this is the content of my console:

Initializing
Setting callback
Paying
Paying

The same happens with prints inside the GetUserInfo().getUserData() and GetUserInfo().getPlans() calls.

It seems that the _initializeWidget future is called only once, however the methods inside it are called twice (once on instantiation and once on the execution of the future I believe).

The main problem I'm having with this is that the startSubscription() method opens a Dialog and, since the method is called twice, the dialog is opened twice.

exaby73 wrote this answer on 2022-09-23

Hi @SeriousMonk
This issue doesn't seem to describe a bug or a feature request. Please see https://flutter.dev/community for resources and asking questions like this,
you may also get some help if you post it on Stack Overflow and if you need help with your code, please see https://www.reddit.com/r/flutterhelp/
Closing, as this isn't an issue with Flutter itself. If you disagree, please write in the comments and I will reopen it.
Thank you

SeriousMonk wrote this answer on 2022-09-23

The issue was caused by another widget triggering the method.

More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-09-30
Star Count 145381
Watcher Count 3565
Fork Count 23363
Issue Count 11208

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date