Home > database >  Is it possible to share and update one screen's reactive value in another screen without Provid
Is it possible to share and update one screen's reactive value in another screen without Provid

Time:01-21

So I have this block of code in a widget that navigates to another screen:

screen_one.dart

class ScreenOne extends StatefulWidget {
  const ScreenOne({ super.key });

  @override
  State<ScreenOne> createState() => _ScreenOneState();
}

class _ScreenOneState extends State<ScreenOne> {
  List<String> state = [''];

  @override
  Widget build(BuildContext context) {
    return Column(
      MaterialButton(
        onPressed: () => Navigator.pushNamed(context, '/screen-two'),
        child: Text('Click here.')
      ),
      Text(state[0]),
    );
  }
}

screen_two.dart

class ScreenTwo extends StatelessWidget {
  const ScreenTwo({ super.key });

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Basically I need to pass the state variable from ScreenOne to ScreenTwo and then update it there (in ScreenTwo)

ScreenTwo needs to display the same thing as ScreenOne and add() a new item to the state list when some button is clicked which should show on both the screens.

Its just one simple List so I am trying to avoid using provider.

Is it possible to do though?

I'm currently just passing it through the Navigator:

screen_one.dart

Navigator.pushNamed(
  context,
  '/post-info',
  arguments: state,
),

screen_two.dart

Widget build(BuildContext context) {
  final List<String> post = ModalRoute.of(context)!.settings.arguments as List<String>;
  // ...
}

CodePudding user response:

first I want to recommend you when things go bigger and more complex, it's better to use a state management approach, However since you did say that you have only one List you can simply use a ValueNotifier, with ValueListenableBuilder: // this should be outside widget classes, maybe in a custom-made class or just in a global scope. ValueNotifier stateNotifier = ValueNotifier([""]);

now in the places you want to use that state, you can use ValueListenableWidget like this:

  ValueListenableBuilder(
      valueListenable: stateNotifier,
      builder: (context, value, child) {
        return Column(
          children: [
            Text('${state[0]}'),
            MaterialButton(
              onPressed: () {
               Navigator.pushNamed(context, '/screen-two'),
              },
              child: Text('click'),
            ),
          ],
        );
      },
    );
  }
}

and any other place where you want to see that state get updates, you need to use ValueListenableWidget.

Now, for executing a method like add() on the List and notify the widgets, you need to assign a new value for it like this:

void addInTheList(String elem) {
   List current = stateNotifier.value;
   current.add(elem);
   
   // this exactly what will be responsible for updating.
   stateNotifier.value = current;
 }

now, you can just call addInTheList and expect it to update in all of them:

addInTheList("Example");
  • Related