fluttergoogle-cloud-firestoredetailview

How do I pass the user info from the detailpage unto another page?


Flutter Firestore - StreamBuilder Users > ListView Users > DetailPage User > Message User

So I have a stream of my Users Collection from Firestore.

On tap it goes to the detailpage (profile) of that user. But now I also want to integrate the possibility to send that user a message. So I need to pass the data from the detailpage to another page. How do I pass the user info from the detailpage unto another page?


Solution

  • When you are you pushing a new page, you pass the data via parameter to the widget, which is pushed, like this:

    Navigator.of(context).push(
       MaterialPageRoute(
           builder: (context) => const NextPage("Passed data"), // We pass here the data
       ),
    );
    

    Full example:

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          home: HomePage(),
        );
      }
    }
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text("Home")),
          body: Center(
            child: ElevatedButton(
              child: const Text("Button"),
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const NextPage("Passed data"), // We pass here the data
                  ),
                );
              },
            ),
          ),
        );
      }
    }
    
    class NextPage extends StatelessWidget {
      final String data;
      
      const NextPage(this.data);
      
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text("Home")),
          body: Center(
            child: Text(data)
          ),
        );
      }
    }
    

    You can run this example under this link: https://dartpad.dev/e0d3b247da70f6080ea5917ce346c5a4