Last active
February 7, 2021 05:49
-
-
Save GAM3RG33K/8fb0631a349a3d183a83111a65c512ae to your computer and use it in GitHub Desktop.
Dart: updating instance's premitive type members
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| final pos = Positions(); | |
| print('\n-------------Issue demo---------------\n'); | |
| updatePositionIssue(pos.top); | |
| print('positionAttr: ${pos.top}'); | |
| updatePositionIssue(pos.left); | |
| print('positionAttr: ${pos.left}'); | |
| updatePositionIssue(pos.right); | |
| print('positionAttr: ${pos.right}'); | |
| updatePositionIssue(pos.bottom); | |
| print('positionAttr: ${pos.bottom}'); | |
| print('\n-------------Possible solution---------------\n'); | |
| updatePositionSolution(() => pos.top, (value) => pos.top = value); | |
| print('positionAttr: ${pos.top}'); | |
| updatePositionSolution(() => pos.left, (value) => pos.left = value); | |
| print('positionAttr: ${pos.left}'); | |
| updatePositionSolution(() => pos.right, (value) => pos.right = value); | |
| print('positionAttr: ${pos.right}'); | |
| updatePositionSolution(() => pos.bottom, (value) => pos.bottom = value); | |
| print('positionAttr: ${pos.bottom}'); | |
| } | |
| updatePositionIssue(double positionAttr) { | |
| // updates the local copy only | |
| positionAttr = 10; | |
| print('positionAttr: ${positionAttr}'); | |
| } | |
| updatePositionSolution(double Function() getPosition, void Function(double newPosition) setPosition) { | |
| //updates actual member value in instance | |
| setPosition(10); | |
| print('positionAttr: ${getPosition()}'); | |
| } | |
| class Positions { | |
| double top; | |
| double left; | |
| double right; | |
| double bottom; | |
| Positions() { | |
| top = 0; | |
| left = 0; | |
| right = 0; | |
| bottom = 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment