skip to Main Content

I want to fetch status from database and show in listview as text but status which i store in database is number, how can i set those status as text?

0 = start
2 = pending approval
3 = Approved

                    return ListView.builder(
                        itemCount: problemlist.length,
                        itemBuilder: ((context, index) {
                           problemModel problem = problemlist[index];
                           return Card(
                            margin: EdgeInsets.all(10),
                            child: ListTile(
                              leading: CircleAvatar(
                                 backgroundImage: NetworkImage(
                                  'http://192.168.1.5/skc/Problem_image/${problem.image}',
                                  ),
                                 ),
                              title: Text(
                                problem.name_surname,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 19),
                              ),
                               subtitle: Text(
                                problem.status,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 17),
                              ),
                            ),
                          );
                        }));
                 

2

Answers


  1. The straigthforward method is to create an helper function to change the type of your value.

        String getStatusText(int statusCode) {
          switch (statusCode) {
            case 0:
              return 'Start';
            case 2:
              return 'Pending Approval';
            case 3:
              return 'Approved';
            default:
              return 'Unknown Status';
          }
        }
    

    Or later on if you have more complex object that not only has a status and manage directly custom types in your app

        class Status {
          final int code;
          final String text;
        
          Status._(this.code, this.text);
        
          factory Status.fromCode(int statusCode) {
            switch (statusCode) {
              case 0:
                return Status._(statusCode, 'Start');
              case 2:
                return Status._(statusCode, 'Pending Approval');
              case 3:
                return Status._(statusCode, 'Approved');
              default:
                return Status._(statusCode, 'Unknown Status');
            }
          }
        }
    
    
    Login or Signup to reply.
  2. create a status enum

    enum StatusEnum{
          Start,
          PendingApproval,
          Approved,
        }
    

    which can be accessed through

    StatusEnum.values[i].name
    

    where ‘i’ is status code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search