skip to Main Content

I need To pass Data to Another flutter Page without navigating

Eg: in page 1 have a button clicking this button should store into selecteddocumentedId

so this documentid can i use in any another flutter page so i can get the data

    class _EeachWorkModelState extends State<EeachWorkModel> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {},

In this on tap

widget.prolist['doc_id']

this should be passed without navigating to another screen

2

Answers


  1. Use a persistent storage maybe like shared_preferences

    Page one, on button event you set a value using a key and on any other page you read it using same key.

    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('some_key', 'doc_id');
    final String? action = prefs.getString('some_key');
    
    Login or Signup to reply.
  2. If you want the ID value to persist even after closing and reopening the app – between app sessions, you need to use shared preference, as described by the earlier contributor.

    Otherwise, you might need to use state management to effectively store the state (i.e. the selectedDocumentID), such as Riverpod or GetIt.

    I personally use Riverpod, and here’s how it will work in Riverpod.

    You will first need to add flutter_riverpod package to you pubspec.yaml

    Read the full initial setup by reading through this highly recommended article. Particularly note the ProviderScope, using ConsumerWidget, Using Consumer and Using ConsumerStatefulWidget & ConsumerState sections.

    Presuming the ID is a String, you can simply use state provider, initiate the ID value as empty String ” as below

    final selectedDocumentIDStateProvider = StateProvider<String>((ref) {return '';});
    

    Then set the document ID value by using

    ref.read(selectedDocumentIDStateProvider.notifier).state = 'example_document_id'
    

    And read the value anywhere within consumer widget using

    final selectedDocumentID = ref.watch(selectedDocumentIDStateProvider);
    

    If your selectedDocumentID is a complex object, or you need to implement a number of methods to the class, you should consider using notifier Provider instead of state provider.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search