728x90

학습목표


목표 : 유지 보수하기 편리한 방식으로 코드 개선하기

 

1. Model 만들기 (알람 정보)

fromMap method와 toMap method를 만들기

필드 추가하기

 

2. Folder Naming, File Naming기능별로 폴더를 만들고, 아래에 _screen.dart 만들기, 구성 요소는 components 폴더에 넣기

 

3. GetXController를 Singleton 방식으로 변경하기 ( 메모리 절약, 미리 생성한 인스턴스 활용 )

최상단 위젯에서 Get.put을 한 후 Get.find를 사용하여 기존 인스턴스 활용하기

 

 

회고


 

목표 : 유지 보수하기 편리한 방식으로 코드 개선하기

1. Model 만들기 (알람 정보)

fromMap method와 toMap method를 만들기

필드 추가하기

 

class AlarmModel {
  // 알람 id
  String id;
  // 사용자
  String userId;
  // 07:00
  String time;
  // 메모
  String memo;
  // 월,수,금 / 평일
  String days;
  // 속한 그룹 아이디
  String groupId;
  // 속한 그룹 이름
  String groupName;
  // 만들어진 날짜
  String insertDate;
  // 수정된 날짜
  String updateDate;
  // 그룹 주인 id
  String groupOwnerId;

  AlarmModel({
    required this.id,
    required this.userId,
    required this.time,
    required this.memo,
    required this.days,
    required this.groupId,
    required this.groupName,
    required this.insertDate,
    required this.updateDate,
    required this.groupOwnerId,
  });
  AlarmModel.fromJson(Map<String, dynamic> json)
      : this(
          id: json['id'],
          userId: json['userId'],
          time: json['time'],
          memo: json['memo'],
          days: json['days'],
          groupId: json['groupId'],
          groupName: json['groupName'],
          insertDate: json['insertDate'],
          updateDate: json['updateDate'],
          groupOwnerId: json['groupOwnerId'],
        );

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'time': time,
      'memo': memo,
      'days': days,
      'groupId': groupId,
      'groupName': groupName,
      'insertDate': insertDate,
      'updateDate': updateDate,
      'groupOwnerId': groupOwnerId,
    };
  }
}

 

class ShareAlarmModel {
  // 속한 그룹 아이디
  String id;
  // 속한 그룹 이름
  String groupName;
  // 삭제, 숨김, 공개 플래그
  String shareState;
  // 만들어진 날짜
  String insertDate;
  // 수정된 날짜
  String updateDate;
  // 그룹 주인 id
  String userId;
  // 그룹에 속한 리스트 json 형태 생각하기
  String textAlarmList;

  ShareAlarmModel({
    required this.id,
    required this.groupName,
    required this.shareState,
    required this.insertDate,
    required this.updateDate,
    required this.userId,
    required this.textAlarmList,
  });

  ShareAlarmModel.fromJson(Map<String, dynamic> json)
      : this(
          id: json['groupId'],
          groupName: json['groupName'],
          shareState: json['groupName'],
          insertDate: json['insertDate'],
          updateDate: json['updateDate'],
          userId: json['userId'],
          textAlarmList: json['textAlarmList'],
        );

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'groupName': groupName,
      'shareState': shareState,
      'insertDate': insertDate,
      'updateDate': updateDate,
      'userId': userId,
      'textAlarmList': textAlarmList,
    };
  }
}

 

2. Folder Naming, File Naming기능별로 폴더를 만들고, 아래에 _screen.dart 만들기, 구성 요소는 components 폴더에 넣기

 

3. GetXController를 Singleton 방식으로 변경하기 ( 메모리 절약, 미리 생성한 인스턴스 활용 )

최상단 위젯에서 Get.put을 한 후 Get.find를 사용하여 기존 인스턴스 활용하기

 

// in GetMaterialApp
final ac = Get.put(AlarmController());

// Singleton
static AlarmController get to => Get.find();

// Usage
AlarmController.to.updateAlarmInfo();

 

+ Recent posts