DavBfr / DavBfr/flutter_pref

PrefPage does not refresh on setState()

Open
#41 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Dart
Stars
17
Forks
10
Avg merge
28m
Merged PRs (30d)
1

Description

First off, thank you for the good work!

I am not completely sure that this is a bug (but it seems to me that it is).

I am using v2.7.1 of the library (2.8.x throws some error not working at all, I will look that up and might open an issue for that as well) to create a settings page within my flutter app. In particular, in the minimum code sample below, I am using the PrefPage widget (which so far seems to be the only widget with this specific problem).

Here is the state part of my minimal app:

class _MyAppState extends State<MyApp> {

  int currentChoice = 0;

  void _updateChoice(int? value) {
    setState(() {
      currentChoice = value!;
    });
  }

  @override
  Widget build(BuildContext context) {
    return PrefService(
      service: widget.service,
      child: MaterialApp(
        home: Scaffold(
            appBar: AppBar(title: const Text('Choice')),
            body: PrefPage(
            children: [PrefPageButton(
                key: UniqueKey(),
                page: PrefPage(cache: false, key: UniqueKey(), children: [
                  PrefDropdown(
                      onChange: _updateChoice,
                      items: const [DropdownMenuItem(value: 0, child: Text("Item 1")), DropdownMenuItem(value: 1, child: Text("Item 2"))],
                      pref: 'choice',
                      title: const Text("Title"),
                      key: UniqueKey()),
                  ((currentChoice ?? 1) == 0) ? PrefText(key: UniqueKey(), pref: "myText") : PrefTitle(key: UniqueKey(), title: const Text("myTitle"))
                ]))])),
      ),
    );
  }
}

Now, when I change the choice in my dropdown item within that widget, the page is not refreshed even though I call an additional setState() on an extra variable currentChoice and the build method gets called. If I go back to the main screen and then back to the PrefPage however, it is updated.

I think it would be best if the widget was updated without using any additional setState() logic on variables apart from the corresponding pref variable, as the currentChoice variable is redundant as it is already stored in the pref preferences.

Interestingly, if I change the PrefDropdown part to a normal DropdownButton by using the following code, it gets even worse as the chosen item is not even shown in the dropdown button (if I choose Item 2 instead of Item 1, the screen still shows Item 1).

DropdownButton(
              onChanged: _updateChoice,
              items: const [DropdownMenuItem(value: 0, child: Text("Item 1")), DropdownMenuItem(value: 1, child: Text("Item 2"))],
              value: currentChoice,
              key: UniqueKey()),
              ((currentChoice ?? 1) == 0) ? PrefText(key: UniqueKey(), pref: "myText") : PrefTitle(key: UniqueKey(), title: const Text("myTitle"))
            ]))])),

I can post the full code of the minimal app if that helps.

Thanks!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the minimal app with PrefPage and PrefDropdown on flutter_pref v2.7.1, focusing on the setState() callback and the PrefPage navigation shown in the issue. Compare the behavior with a normal DropdownButton and verify whether returning to the page is the only refresh trigger. Done means the selected value and conditional PrefText or PrefTitle update immediately without redundant state.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.