google / flutter.widgets

https://pub.dev/packages/flutter_widgets
BSD 3-Clause "New" or "Revised" License
1.37k stars 474 forks source link

[linked_scroll_controller] error when being set via setState #539

Open deathblade666 opened 4 weeks ago

deathblade666 commented 4 weeks ago

Problem description

linked_scroll_controller

Receive this error when trying do .addAndGet() via setState() instead of intitState(). the purpose for me to set it via setState() is so that it can be toggleable in case user's dont want synchronized scrolling across the widgets. for application context, I'm making a Markdown editor as (top of the screen is the user's input rendered inmarkdown, bottom is a textfield) my first flutter app and the thought behind using this package is that if someone is editing a large file being able to have scroll synchronized would be helpful in making sure it renders properly as well as finding sections to edit.

Anyway, after I get this error the app and this package function as expected after a restart (thanks to sharedd preferences loading on start), however I dont think having to restart the app is good UX.

image

Steps to reproduce

Not sure what to put here so I'll just put my code snippet, this function is also called upon via initState() and thus the reason for the extra checks, as initState loads the last known settings from shared preferences.

void syncScroll (syncScrolling){
    if (syncScrolling == true) {
      setState(() {
        scrollRenderController = scrollGroup.addAndGet();
        userInputController = scrollGroup.addAndGet();
      });
    }
    if (syncScrolling == false) {
      if (userInputController.hasClients == true){
        setState(() {
          userInputController.dispose();
        });
      }
      if (scrollRenderController.hasClients == true){
        setState(() {
          scrollRenderController.dispose();
        });
      }
    }
  }

Expected behavior

to not error

Actual behavior

error (picture above)

Environment

Tested on Linux (Arch) as well as Android (pixel 6 running latest android 15 beta)

Additional details