skip to Main Content

I’m a newbie Flutter programmer.
Im trying to make a generic grid widget that let me pass an object and create columns and rows dynamically.

I’ve made it work with an specific object but what i need is to read the names of attributes of an object to dynamically create grid column names and values so i can use the same grid for client or articles or everything.

For example i have a class for clients

class Client {
  int id;
  String name;
  String mail;

  Client({required this.id, required this.name, required this.mail});
}

then in my code retrieve a list of clients and pass that to the grid widget as List.

I just need to know how to loop the object recieved to:

  1. get the list of fields names (id, name, mail)
  2. then get its value for example something like
    var field_name = element; 
    obj.field_name.value;

Hope you can understand what im trying to do.
Thank you in advance.

2

Answers


  1. you can try this code

    FutureBuilder<List<Client>>(
                future: // your future method which return the List<Client>,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return ListView.builder(
                      itemCount: snapshot.data!.length,
                      itemBuilder: (context, index) {
                        final client= snapshot.data![index];
                        return ListTile(
                          title: Text(client.name),
                          leading: CircleAvatar(
                            child: Text(client.id),
                          ),
                          subtitle: Text(client.email),
                        );
                      },
                    );
                  } else if (snapshot.hasError) {
                    return Center(
                      child: Text(snapshot.error.toString()),
                    );
                  }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              },),
    

    let me know if this is working for you.

    Login or Signup to reply.
  2. Say you are getting List of Client Object and Class looks like this

    class Client {
      int id;
      String name;
      String mail;
    
      Client({required this.id, required this.name, required this.mail});
    }
    

    Now if your question is how can i differentiate some Client from the list and have a separate Grid widget for them?

    If yes, lets take example with above given class
    We have Client with category, Design, Development etc.

    We can simply have a another variable in class such as

    class Client {
      int id;
      String name;
      String mail;
      //type variable to differentiate , could be String or enum if you know exactly 
      String type;
      Client({required this.id, required this.name, required this.mail});
    }
    

    and

    GridView.builder(  
                  itemCount: images.length,  
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(  
                      crossAxisCount: 2,  
                      crossAxisSpacing: 4.0,  
                      mainAxisSpacing: 4.0  
                  ),  
                  itemBuilder: (BuildContext context, int index){  
    
                    Client item = Clients[index]; 
                    if(item.type == 'Design')
                    {
                        // display designClient item
                        return DesignGrid();
                    }
                      if(item.type == 'Development')
                    {
                        // display developmentType item
                        return DevelopmentGrid();
                    }
                  }
                )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search