Flutter Send Data to Screen with Example

In Flutter, passing data between screens can be accomplished using arguments with named routes.

Here's an example of how to send data to a screen in Flutter

Define a named route for the screen that will receive the data

 MaterialApp(
  routes: {
    '/screen2': (context) => Screen2(),
  },
);
In this example, we are defining a named route for a screen called Screen2.

Add arguments to the named route by passing a Map<String, dynamic> object

 Navigator.pushNamed(context, '/screen2', arguments: {
  'data': 'Hello, world!',
});
In this example, we are passing a Map<String, dynamic> object as the arguments parameter when navigating to Screen2. The object contains a single key-value pair with a string key and string value.

Receive the arguments in the screen that will receive the data

 class Screen2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final arguments = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
    final data = arguments['data'];
    return Scaffold(
      appBar: AppBar(
        title: Text('Screen 2'),
      ),
      body: Center(
        child: Text(data),
      ),
    );
  }
}
In this example, we are retrieving the arguments passed to Screen2 by using the ModalRoute.of(context)!.settings.arguments property, which returns the arguments as a dynamic object. We are then casting the arguments to a Map<String, dynamic> object and retrieving the data using the key data.

Run your app and test the data send

 ​flutter run
This is just a basic example of how to send data to a screen in Flutter. You can customize the implementation to fit your app's specific needs, such as passing different types of data or using multiple key-value pairs in the Map<String, dynamic> object.