We can pass data to named route in three ways. Map, List, Extra Class.
final args = Get.arguments as Map<String, String>; final name = args['name'];
List => Arguments as list
final args = Get.arguments as List?; final name = args?[0] ?? 'default if not given'; // Arguments as map or list appBar: AppBar(title: Text('Screen One $name')), // Directly pass arguments as List here appBar: AppBar(title: Text('Screen One ${Get.arguments[2]}')),
Separate Class => Make a separate class and use it
// Define a class class ScreenOneArguments { final String name; final int age; final String profession;
ScreenOneArguments({ required this.name, required this.age, required this.profession, }); }
// Passing arguments Get.toNamed( '/ScreenOne', arguments: ScreenOneArguments( name: 'Muhammad Bilal Akbar', age: 25, profession: 'Flutter Developer', ), );
// Accessing arguments final args = Get.arguments as ScreenOneArguments; final name = args.name; final age = args.age; final profession = args.profession;
Use a Map: For small, simple use cases where type safety isn't critical. When you need flexibility in the data structure.
Use a List: For very simple use cases with a small, fixed number of arguments. When you don't need descriptive keys or type safety.
Use a Custom Class: For larger, more complex use cases where type safety and maintainability are important. When you want to enforce a clear structure for your arguments.
Passing arguments as a List is indeed faster and simpler, especially for small and fixed parameters. However, it has some drawbacks:
Pros of Passing Arguments as a List ✅ Less code (no need to define keys like in a Map). ✅ Slightly better performance (accessing Get.arguments[0] is a direct lookup). ✅ Good for small, fixed parameters (e.g., an ID and a name). Cons of Passing Arguments as a List ❌ Hard to understand later (without looking at the sender’s code, it's unclear what Get.arguments[1] means). ❌ Order-sensitive (if you change the order, the app might break). ❌ Not good for optional parameters (e.g., if only the second argument is needed, the list index might be null).
When is it "Better"? If you're passing only 1-2 arguments, and their order will never change. Example: Get.toNamed('/details', arguments: [42, 'Product Name']); (ID and Name). When is it "Not Recommended"? If you need more than 2-3 parameters, since remembering indexes (Get.arguments[3]) becomes difficult. If your app needs optional parameters, since missing values can cause crashes (Get.arguments[3] might not exist).