본문 바로가기

Flutter-플러터/클론코딩

[클론코딩]레시피 계산기 -1 편 Json

반응형

JanVeb/recipe_calculator at flutterawesome.com (github.com)

 

GitHub - JanVeb/recipe_calculator

Contribute to JanVeb/recipe_calculator development by creating an account on GitHub.

github.com

 

 

위 코드를 클론코딩해보았습니다. 

 

만들고 싶은 원가계산기의 뼈대가 될 것 같습니다.

 

하지만 코드가 아직 너무 어려워서 오늘은 Json 부분 코드만 연습했습니다. 

 

 

 

 

배운점 

 

1. 플러터에서 Json 사용하기

class Ingredient {

  String name;
  double amount;
  late double amountCalculated;
  String unit;
  bool isEditable = false;

  //틀을 만드는 작업
  Ingredient(this.name, this.amount, this.amountCalculated, this.unit,
      this.isEditable);


//json스트링을 객체로 파싱하는 방법
  Ingredient.fromJson(Map<String, dynamic>json)
    : name ='',
    amount = 0,
    amountCalculated=0,
    unit = ''
  {
    name= json['name'];
    amount= json['amount'];
    amountCalculated=json['amountCalculator'];
    unit = json['unit'];
    }


    //객체를 json 스트링으로 변환
    Map<String, dynamic>
    toJson()=> {
    'name': name,
    'amount': amount,
    'amountCalculatored' : amountCalculated,
    'unit': unit
  };

}

 

 

 

 

 

 

 

 

 

반응형