RestaurantCard 에서 String? detail 을 추가하는 부분에서 질문드립니다.
397
21 asked
isDetail 여부에 따라 String? detail 을 적용시켜주는 부분에서 질문 드립니다.
팩토리를 만들때 원래 클래스에서 받는 내용을 그대로 받는 것으로 알고 있었는데 RestaurantCard 에서 선언한
final String? detail;과
컨스트럭터 parameter 인
this.detail;이 factory 에서는 받아오지 않아도 되는 것인가요?
factory RestaurantCard.fromModel({
required RestaurantModel model,
bool isDetail = false,
String? detail, <-이부분
}) {
return RestaurantCard(
image: Image.network(
model.thumbUrl,
fit: BoxFit.cover,
),
name: model.name,
tags: model.tags,
ratings: model.ratings,
ratingsCount: model.ratingsCount,
deliveryTime: model.deliveryTime,
deliveryFee: model.deliveryFee,
isDetail: isDetail,
detail: detail, <-이부분
);
}스스로 해볼때
<- 이부분 이라고 표시한 부분도 포함해서 저는 작성해보았었습니다. 작동도 잘 되구요..
굳이 입력을 하지 않아도 되는 이유가 알듯말듯한데 확실하지가 않습니다. 그리고 저처럼 했을때 어떤 문제가 있는 것인가요?
감사합니다.
Answer 2
0
안녕하세요!
그렇게 작업하셔도 문제 없지만 사실상 중복입니다.
나중에 아래와같이 코드를 변경합니다.
factory RestaurantCard.fromModel({
required RestaurantModel model,
bool isDetail = false,
}) {
return RestaurantCard(
image: Image.network(
model.thumbUrl,
fit: BoxFit.cover,
),
heroKey: model.id,
name: model.name,
tags: model.tags,
ratingsCount: model.ratingsCount,
deliveryTime: model.deliveryTime,
deliveryFee: model.deliveryFee,
ratings: model.ratings,
isDetail: isDetail,
detail: model is RestaurantDetailModel ? model.detail : null,
);
}detail은 모델에 있는 값이기때문에 추가로 입력해줄 필요가 없습니다.
0
그리고 추가로, 해당 detail 을 적용 할때 if 조건문이
if(detail != null && isDetail)에서 그냥...
if(isDetail) 만 해도 되는 것이 아닌가 생각되는데 이렇게 하면 어떤 잘못이나 파생되는 문제가 있는지도 추가로 여쭤보고 싶습니다. 감사합니다.
0
boolean과 null은 다른값입니다. if(null)은 다트언어에서 불가능한 문법입니다. JS에서 오셨다면 헷갈리실 수 있으나 이부분은 사실 JS가 특이한 부분이고 대부분의 언어가 Dart언어와 같습니다.
자동 패키지 임포트 관련 질문 드립니다.
0
642
1
초급 중급 마무리한 학생입니다.. 파베관련 질문 하나만 받아주실 수 있을까요
0
429
1
Pagination 질문드립니다
0
460
1
레이아웃에 대해서 궁금한 질문!
0
482
1
강의에 적용된 디자인 패턴이 뭔가요??
1
666
1
네스팅 방식으로 goRouter Go를 사용할때, 앱바타이틀이 정상적으로 표시되지 않는 문제
0
544
2
GoRouter return이 안됩니다.
0
589
1
UserModel 에서는 IModelWithId를 implements하지 않는 이유가 있나요?
-1
530
1
서버쪽 /user/me api에서 에러가 나서 확인해보니..
0
399
1
[에러] GoRouter 적용, 401 에러가 뜹니다
0
356
1
ref.read.update // state + 1
0
444
1
typedef itemBuilder 제네릭 관련 질문이 있습니다.
0
446
1
[상태관리] Riverpod을 쓰신 이유가 있으신가요?
0
663
1
Dio 질문드립니다.!
0
763
2
CursorPagination 캐스팅 관련 문의
0
403
1
CursorPagination 캐시팅 관련 문의
0
385
2
AuthRepository 클래스 token() 함수는 Dead Code 아닌가요?
0
353
1
Rating을 GET 요청할때 DioError가 발생합니다.
-1
601
1
Token 관련 질문 드립니다.
0
440
2
수업의 내용은 아니지만 궁금해서 여쭤봅니다.
0
568
1
refresh 토큰 재발급 요청에 대해서
0
1681
1
_App 클래스가 ConsumerWidget을 상속 받으면 에러가 생깁니다
0
2531
4
이번 플러터와 다트 업데이트에서
1
261
1
Dio로 api 요청 듣고 있는데 에러가 발생합니다 ㅜㅜ
0
1164
1

