Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which code should I use to provide the ID to another StatelessWidget?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

I think you should consider these points:

  1. If you need to use other properties from the model (may be like name, address etc.) then you should pass the model
  2. If you need only the id property from the model then you should pass the id only
  3. 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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading