I want to give the ID to another StatelessWidget
. But, there is two way for me to choose.
The first way is to use the model's ID. The data for this model comes from a Firebase Cloud Firestore document and I use the original ID as the document id.
The second way is to use the original ID.
Which way should I choose?
My code:
class Model {
Model(this.id, …);
final String id;
…
factory Model.fromMap(Map<String, dynamic> data, String documentId) {
…
return Model(documentId, …);
}
Map<String, dynamic> toMap() => {…};
}
class AccountScreen extends StatelessWidget {
const AccountScreen({required this.id, super.key});
final String id; // Original ID
@override
Widget build(BuildContext context) {
return … // Widget for getting data from a Firebase Cloud Firestore document and converting it into a model.
Scaffold(
appBar: AppBar(title: Text("Account")),
body: SingleChildScrollView(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.grey, width: 0.5), borderRadius: BorderRadius.circular(10.0)),
child: Card(
margin: EdgeInsets.zero,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
elevation: 0.0,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0),
child: Align(
alignment: Alignment.topLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(16.0),
child: Text(
"Summary",
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0),
),
),
ListTile(
onTap: () => …, // Pass the ID to another StatelessWidget
title: const Text(
"Profile",
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: Colors.grey, fontSize: 16.0),
),
trailing: const Icon(Icons.arrow_forward_ios, color: Colors.cyan),
),
],
),
),
),
),
),
],
),
),
);
}
}
Should I use model.id
or id
?
Feel free to leave a comment if you need more information.
CodePudding user response:
I think you should consider these points:
- If you need to use other properties from the model (may be like name, address etc.) then you should pass the model
- If you need only the id property from the model then you should pass the id only
- If you think, in future you will need other properties then you should pass the model property obviously. In this approach you don't have to worry about future changes/refactor.