Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

state management - Flutter and GetX - Widget doesn't redraw when I update my RxObject

Here the existant :

My IssueController

Rx<GitlabIssue> selectedIssue = new GitlabIssue().obs;
RxList<GitlabIssue> issues = <GitlabIssue>[].obs;


void changeIssueState(GitlabIssueState state) async {
var stateValue = _getGitLabIssueStateValue(state);
bool isInternet = await Utils().isInternet();
if (isInternet) {
  var resp = await _issueProvider.changeGitlabIssueState(
      projectId, selectedIssue.value.iid, stateValue);

  if (resp.statusCode == 200) {
    Get.snackbar(
      'Issue state changed',
      'Issue is $stateValue',
    );
    var issueIndex = issues.indexOf(selectedIssue.value);
    selectedIssue.value.state =
        selectedIssue.value.state == 'opened' ? 'closed' : 'opened';
    issues[issueIndex] = selectedIssue.value;

    gitLabIssueBox.put(projectId, issues);
  } else
    Get.snackbar(
        'Issue state not changed', 'Issue state was not correctly changed');
} else
  print('No connectivity');
}

My view

 Widget _getButtonStatus() {
return Obx(() {
  Color stateColor = _issueController.selectedIssue.value.state == 'closed'
      ? Colors.green
      : Colors.red;
  return OutlinedButton(
    onPressed: () {
      _issueController.selectedIssue.value.state == 'closed'
          ? _issueController.changeIssueState(GitlabIssueState.reopen)
          : _issueController.changeIssueState(GitlabIssueState.close);
    },
    style: OutlinedButton.styleFrom(
      //primary: Colors.white,
      //backgroundColor: Colors.teal,
      side: BorderSide(color: stateColor, width: 1),
    ),
    child: Obx(
      () => Text(
        _issueController.selectedIssue.value.state == 'closed'
            ? 'Reopen issue'
            : 'Close issue',
        style: TextStyle(color: stateColor),
      ),
    ),
  );
});
}

I update the value of my selectedIssue.obs and I put the widget in an Obx() => function.

I can't figure out why my widget doesn't redraw.

Weirdly, my onPressed function behave correctly; The button stay the same but if I click again on it the status of my issue change correctly.

Please help me !! :D


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...