In flutter I have been using ToggleButtons (https://api.flutter.dev/flutter/material/ToggleButtons-class.html) with MVC architecture. An http request is send from the controller. I need to update a ToggleButton based on the response. The ToggleButton is changed with a list. Example: isSelected = [false, true];
How should i do it? Should I need to Change the boolean value.
CodePudding user response:
your toggle button should be inside of an statefull widget. In the state class you have to create a field for your boolean eg. _isSelected
this field should be used for your toggle and your updates... after the response set your value and call setState() to refresh the whole widget
CodePudding user response:
As an alternative to the solution provided by Raegtime, you can consider using a StreamBuilder with a stream that periodically sends the HTTP request and builds your ToggleButtons accordingly, or if you only need to send the request once you can use a FutureBuilder instead. This way you won't have to use StatefulWidgets.
If you keep your business logic (that is, the function that does the HTTP request, or even the function that gets you the stream) in a separate file, this can offer a clean alternative, especially since you mention wanting to use a MVC pattern.
