FutureBuilder method inside future called twice

This issue has been created 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-10-05
Star Count 145516
Watcher Count 3569
Fork Count 23400
Issue Count 11194

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
We don't need to expose ssh port for Docker 1 2020-09-17 2022-05-16
Dockerfile for installing tigergraph 3.1.0 is not working 4 2021-02-04 2022-07-19
Integration canDrive with OP-COM Interface 2 2020-11-29 2022-07-17
Binaries with a dash char as part of their filenames fails to command-complete when using `clink.argmatcher()` 11 2021-11-10 2022-07-19
zlib.error: Error -5 3 2022-03-10 2022-09-25
"mportError: cannot import name 'Document' from 'borb.pdf.document'" 1 2022-03-03 2022-09-25
Get-DbaAgReplica add Availability Group to output 1 2021-12-02 2022-08-25
Azure - AppService Initial Requests 0 2021-09-23 2022-08-16
Fix Model broken - .py width error 3 2022-03-30 2022-08-28
Explorer should build without Sentry credentials defined 8 2021-10-08 2022-07-22
Platform Specific Provisioner Settings 2 2021-10-08 2022-10-02
$filter not working when using with $expand 2 2021-08-02 2022-08-06
Crash on Fabric.with 0 2019-05-30 2022-09-16
When using data-image-list: crashes because of requestResizedImages() function 10 2021-12-15 2022-10-05
disable zoom 0 2022-01-20 2022-01-16
Close Fullsceen? 1 2021-04-27 2022-01-16
If a BigInt/Int field exists on update, the record is not updated. 18 2022-01-06 2022-09-27
How to make web links in messages clickable? 0 2021-12-14 2022-09-12
Setup the Application on Vercel + Add App SEO 3 2021-03-07 2022-08-13
Request for dataset access 2 2021-10-27 2022-09-21
request for data access 2 2021-10-16 2022-09-21
[Feature Request] Add a `--register-app` subcommand for Linux, like Tor Browser does 2 2022-04-09 2022-09-15
XlsxWriter 1 2021-06-03 2022-01-15
in-toto verify-blob verification fails to find the tlog entry. Mismatch in index vs. search 1 2022-02-01 2022-09-14
Performance issue. Calling removeAllEnpoints method does not delete some endpoints data from DOM. 4 2022-02-16 2022-08-27
br add more system tables in mysql schema to black list 1 2021-11-11 2022-07-25
Propagate log level to client 0 2021-11-10 2022-07-13
[CI/CD] pre-release fails due to version check 2 2021-01-11 2022-09-29
Request: CodeSignal 5 2021-09-25 2022-01-21
Remove "Live" label from NWS Smoke widget time slider 2 2021-07-26 2022-10-02
播放时卡顿严重,怎么办 0 2021-01-09 2022-01-15
Make the fast field cache size and the split footer cache size configurable 0 2021-11-05 2022-09-16
Pipeline terminate with killing some actors but not all of them 1 2021-11-05 2022-10-02
docs: Getting Started: Login Using CLI. Add `echo` to avoid printing `%` 1 2021-10-04 2022-09-17
Java集合使用注意事项总结 页面生成有问题 2 2021-11-26 2022-09-19
dummy issue 0 2021-11-01 2021-12-23
can NullAway use CollectionUtils.isEmpty as a valid null check? 5 2020-12-17 2022-09-01
Spring style of initializing process test coverage rule fails to generate a class level report 8 2017-03-09 2022-10-04
paddlex的评估接口evaluate中没有use_vdl参数接口,请问该如何绘制验证集的loss曲线 2 2022-05-19 2022-09-10
Where is the .lib file and headers located after build from source? 6 2022-03-03 2022-09-15
[Settings] FancyZones zones preview doesn't have a disabled state 1 2021-12-26 2022-08-20
it lose the font 'Cascadia Mono' after auto-update 8 2021-10-12 2022-09-28
Error while instantiating the ngo chaincode 5 2019-02-27 2022-09-13
Ledger device unit tests 0 2021-11-17 2022-09-16
Remove worker task TTReadUnsealed 0 2022-01-28 2022-09-26
Some characters such as russian letters are converted which makes it impossible to perform a search query from website 0 2021-04-23 2022-06-21
Long content scrolling in v5.2.0 Modal does not work 6 2022-05-07 2022-09-20
Scheduled Model Regression Test Failed 0 2021-10-18 2022-10-01
Wrong character of @angular/core-Component 4 2022-08-12 2022-10-03
[flutter_markdown]: Release v0.6.6 0 2021-08-31 2022-09-26