728x90
๋ฐ˜์‘ํ˜•

LimitedBox class

์ด ์œ„์ ฏ์€ child์— ๋Œ€ํ•œ ์ œํ•œ์ด ์—†๋Š” ๊ฒฝ์šฐ์— ๋ฐ•์Šค๋ฅผ ์ œํ•œํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
๋Œ€๋ถ€๋ถ„ ์ƒ์œ„ ์š”์†Œ์—์„œ ์ •์˜๋œ ์ œํ•œ์— ๋”ฐ๋ผ ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ง„๋‹ค. ํ•˜์ง€๋งŒ ListView, Column, Row์™€ ๊ฐ™์€ ๊ฒฝ์šฐ, ํ•˜์œ„์š”์†Œ ํฌ๊ธฐ์— ์ œํ•œ์„ ๋‘์ง€ ์•Š๋Š”๋‹ค.

์ด์ฒ˜๋Ÿผ ์ƒ์œ„ ์š”์†Œ์˜ ์ œํ•œ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ, ์ƒ์œ„ ์š”์†Œ ์ œํ•œ์— ์˜์กดํ•˜๋Š” ์œ„์ ฏ์—๋Š” ์–ด๋–ป๊ฒŒ ๊ธฐ๋ณธ๊ฐ’์„ ์ค„ ์ˆ˜ ์žˆ์„๊นŒ?

์ด๋Ÿฐ ๊ฒฝ์šฐ์— LimitedBox๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค!
LimitedBox๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด maxHeight์™€ maxWidth๊ฐ€ ์ œ๊ณต๋œ๋‹ค. ์ด๋Š” child์—๊ฒŒ ๋ฌดํ•œํ•œ ์ƒํ™ฉ์—์„œ ์ž์—ฐ์Šค๋Ÿฌ์šด ํฌ๊ธฐ๋ฅผ ์ œ๊ณตํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค.

๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ  ์ •๋ฆฌํ•ด๋ณด๋ฉด,

๋งŒ์•ฝ ์œ„์ ฏ์˜ ์ตœ๋Œ€ ๋„ˆ๋น„๊ฐ€ ์ œํ•œ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ด ์œ„์ ฏ์˜ child์˜ ๋„ˆ๋น„๋Š” maxWidth์— ๋”ฐ๋ผ ์ œํ•œ๋œ๋‹ค.

๊ฐ™์€ ๋…ผ๋ฆฌ๋กœ, ๋งŒ์•ฝ ์œ„์ ฏ์˜ ์ตœ๋Œ€ ๋†’์ด๊ฐ€ ์ œํ•œ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ด ์œ„์ ฏ์˜ child์˜ ๋†’์ด ๋˜ํ•œ maxHeight์— ์ œํ•œ๋œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

IntrinsicWidth class

๋ฐ”๋กœ ์ด์ „์— IntrinsicHeight class์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์—ˆ๋‹ค.
height๊ฐ€ ์žˆ์œผ๋‹ˆ width๋„ ์กด์žฌํ•œ๋‹ค.

IntrinsicHeight class์™€ ๊ฑฐ์˜ ๋น„์Šทํ•œ ๋‚ด์šฉ์ด๋‹ค.
์œ„ ์„ค๋ช…์—์„œ height๋ฅผ width๋กœ ๋ฐ”๊ฟ”์„œ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.


์ด ์œ„์ ฏ์€ ๋„ˆ๋น„๊ฐ€ ์ œํ•œ๋˜์ง€ ์•Š๊ณ  ๋ฌดํ•œ ํ™•์žฅ์„ ์‹œ๋„ํ•˜๋Š” child๋ฅผ ํ•„์š”๋กœ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

์ด ์œ„์ ฏ์ด child์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” constraints๋Š” ๋ถ€๋ชจ์˜ constraints๋ฅผ ์ง€ํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ constraints๊ฐ€ child์˜ ์ตœ๋Œ€ ๊ณ ์œ  ๋†’์ด๋ฅผ ํฌํ•จํ•  ๋งŒํผ ์ถฉ๋ถ„ํžˆ ํฌ์ง€ ์•Š์œผ๋ฉด ๋น„๊ต์  ๋‚ฎ์€ ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์ตœ์†Œ ๋†’์ด constraints๊ฐ€ child์˜ ์ตœ๋Œ€ ๊ณ ์œ  ๋†’์ด๋ณด๋‹ค ํฌ๋ฉด ๋น„๊ต์  ๋” ๋งŽ์€ ๋†’์ด๊ฐ€ ์ง€์ •๋œ๋‹ค.

  • ๋ถ€๋ชจ์˜ constraints < child์˜ ๊ณ ์œ  ๋„ˆ๋น„
    ๋‚ฎ์€ ๋„ˆ๋น„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.
  • ๋ถ€๋ชจ์˜ constraints > child์˜ ๊ณ ์œ  ๋„ˆ๋น„
    ๋†’์€ ๋„ˆ๋น„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

์ฆ‰, child๋Š” ๋ถ€๋ชจ์˜ constraints๋ฅผ ๋”ฐ๋ฅด๋Š”๋ฐ, ์ด ๋•Œ ๋ถ€๋ชจ์˜ constraints์™€ child์˜ ๊ณ ์œ  ๋„ˆ๋น„ ์‚ฌ์ด์˜ ๊ด€๊ณ„์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค.

์ด class ๋˜ํ•œ ๊ฐ€๋Šฅํ•œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

<์‚ฌ์šฉ ํ›„>

(์‚ฌ์ง„์ถœ์ฒ˜: https://bsscco.github.io/posts/flutter-layout-widgets/)

728x90
๋ฐ˜์‘ํ˜•

'๐Ÿ’ง flutter' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Flutter] Offstage class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] LimitedBox class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] IntrinsicHeight class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] FractionallySizedBox class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] FittedBox Class ์ •๋ฆฌ  (0) 2022.07.06
728x90
๋ฐ˜์‘ํ˜•
728x90

 

IntrinsicHeight class

์ด๋ฆ„์„ ๋ณด์ž๋งˆ์ž Intrinsic์ด ๋ฌด์Šจ ์˜๋ฏธ์ง€? ํ•˜๋Š” ์งˆ๋ฌธ์ด ์ƒ๊ฒผ๋‹ค.

์ฐพ์•„๋ณด๋‹ˆ Intrinsic๋Š” ๊ณ ์œ ํ•œ, ๋ณธ์งˆ์ ์ธ ์ด๋ผ๋Š” ๋œป์ด๋‹ค.

๋œป ๊ทธ๋Œ€๋กœ IntrinsicHeight๋Š” child์˜ ๊ณ ์œ  ๋†’์ด์— ๋งž์ถฐ child์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜๋Š” ์œ„์ ฏ์ด๋‹ค.

์ด ์œ„์ ฏ์€ ๋†’์ด๊ฐ€ ์ œํ•œ๋˜์ง€ ์•Š๊ณ  ๋ฌดํ•œ ํ™•์žฅ์„ ์‹œ๋„ํ•˜๋Š” child๋ฅผ ํ•„์š”๋กœ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

์ด ์œ„์ ฏ์ด child์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” constraints๋Š” ๋ถ€๋ชจ์˜ constraints๋ฅผ ์ง€ํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ constraints๊ฐ€ child์˜ ์ตœ๋Œ€ ๊ณ ์œ  ๋†’์ด๋ฅผ ํฌํ•จํ•  ๋งŒํผ ์ถฉ๋ถ„ํžˆ ํฌ์ง€ ์•Š์œผ๋ฉด ๋น„๊ต์  ๋‚ฎ์€ ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์ตœ์†Œ ๋†’์ด constraints๊ฐ€ child์˜ ์ตœ๋Œ€ ๊ณ ์œ  ๋†’์ด๋ณด๋‹ค ํฌ๋ฉด ๋น„๊ต์  ๋” ๋งŽ์€ ๋†’์ด๊ฐ€ ์ง€์ •๋œ๋‹ค.

  • ๋ถ€๋ชจ์˜ constraints < child์˜ ๊ณ ์œ  ๋†’์ด
    ๋‚ฎ์€ ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.
  • ๋ถ€๋ชจ์˜ constraints > child์˜ ๊ณ ์œ  ๋†’์ด
    ๋†’์€ ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

์ฆ‰, child๋Š” ๋ถ€๋ชจ์˜ constraints๋ฅผ ๋”ฐ๋ฅด๋Š”๋ฐ, constraints๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด ๋•Œ ๋ถ€๋ชจ์˜ constraints์™€ child์˜ ๊ณ ์œ  ๋†’์ด ์‚ฌ์ด์˜ ๊ด€๊ณ„์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค.

๊ฐ€๋Šฅํ•œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

<์‚ฌ์šฉ ์ „>

<์‚ฌ์šฉ ํ›„>

 

 

728x90
๋ฐ˜์‘ํ˜•
๋ฐ˜์‘ํ˜•

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•
728x90

FractionallySizedBox class

FractionallySizedBox class๋Š” ์ƒ์œ„ ๋ถ€๋ชจ์˜ ์‚ฌ์ด์ฆˆ ๋น„์œจ์— ๋งž๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.
์ฆ‰ ๋””์ž์ธ์˜ ํฌ๊ธฐ๊ฐ€ ์ƒ๋Œ€์ ์ด์–ด์•ผ ํ•  ๋•Œ ํฌ๊ธฐ๋ฅผ ์ƒ์œ„ ๋ถ€๋ชจ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋น„์œจ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

example)

  • FractionallySizedBox๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒ์œ„ ๋ถ€๋ชจ์˜ ์‚ฌ์ด์ฆˆ ๋น„์œจ์„ ์ง€์ •ํ•˜์—ฌ child์˜ ํฌ๊ธฐ๋ฅผ ์ •ํ•˜๊ณ  ์žˆ๋‹ค.
  • ์ฝ”๋“œ์—์„œ 0.7์€ 70%๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ฆ‰ ์ƒ์œ„ ๋ถ€๋ชจ ์‚ฌ์ด์ฆˆ์˜ 70%๋งŒํผ child๋ฅผ ๋งŒ๋“ค๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
  • widthFactor์ฒ˜๋Ÿผ heightFactor์„ ์‚ฌ์šฉํ•˜์—ฌ ๋†’์ด์˜ ๋น„์œจ๋„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


์ด์ฒ˜๋Ÿผ FractionallySizedBox class๋Š” ์ƒ์œ„ ๋ถ€๋ชจ์˜ ์‚ฌ์ด์ฆˆ ๋น„์œจ๋กœ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

๋ฐ˜์‘ํ˜•

 

728x90
๋ฐ˜์‘ํ˜•

'๐Ÿ’ง flutter' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Flutter] IntrinsicWidth class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] IntrinsicHeight class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] FittedBox Class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] CustomSingleChildLayout ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] Initializer lists ์ •๋ฆฌ  (0) 2022.07.06
728x90
๋ฐ˜์‘ํ˜•

FittedBox Class

child ๋˜๋Š” ๋‚ด์šฉ์˜ ์–‘์— ๋”ฐ๋ผ ํฌ๊ธฐ๊ฐ€ ํ™•์žฅ๋˜๋Š” ์œ„์ ฏ์— ์˜ํ•ด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒ๋˜๋Š” ๊ฒฝ์šฐ FittedBox๋กœ ๊ฐ์‹ธ๋ฉด ์ „์ฒด ํฌ๊ธฐ๋ฅผ ๋„˜์ง€ ์•Š๊ฒŒ ๋˜๋ฉฐ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐฉ์ง€๋œ๋‹ค.

alignment๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๋ฐฐ์—ด๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

example)



์‚ฌ์šฉ ์ „)

์‚ฌ์šฉ ํ›„)

์ด์ฒ˜๋Ÿผ ๋ถ€๋ชจ์—์„œ ์ฃผ์–ด์ง„ ํฌ๊ธฐ ๋‚ด์— child๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

fittedBox๋Š” ๋ถ€๋ชจ ํฌ๊ธฐ์— ๋งž์ถฐ child ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
BoxFit.cover๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ถ€๋ชจ ํฌ๊ธฐ์— ๋”ฑ ๋งž๊ฒŒ child ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ ) https://medium.com/flutter-community/flutter-widgets-boxes-part-2-the-whole-picture-4bb142f0b1ab

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

CustomSingleChildLayout

childe์˜ ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ •ํ•˜๋Š” ์œ„์ ฏ์ด๋‹ค.

  • delegate์€ child ๋ ˆ์ด์•„์›ƒ์˜ constraints๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ child๋ฅผ ์–ด๋””์— ๋‘˜์ง€, parent์˜ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ํ•˜์ง€๋งŒ parent๋Š” child์˜ ํฌ๊ธฐ์— ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • CustomSingleChildLayout์€ delegate๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
  • ์ฃผ๋กœ grid๋ฅผ ๋งŒ๋“ค ๋•Œ ์“ฐ์ธ๋‹ค.

์ •๋ฆฌํ•˜๋ฉด, child์˜ ์œ„์น˜, ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” layout ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

 

example 1)

const CustomSingleChildLayout({
    Key key,
    @required this.delegate,
    Widget child,
  })

 

example 2)

Widget single(){
  return CustomSingleChildLayout(delegate: delegate)
}
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

Initializer lists

constructor ์‹คํ–‰ ์ „์— final fields๊ฐ€ ๊ฐ’์„ ๊ฐ€์ง€๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋“ฑ์˜ ์ดˆ๊ธฐ ์„ค์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ฆ‰, default field์— ์‹คํ–‰ ์ „์— ๊ฐ’์ด ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด Initializer lists์—์„œ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

Point.fromJson(Map<String, double> json)
    : x = json['x']!,
      y = json['y']! {
  print('In Point.fromJson(): ($x, $y)');
}

assert๋Š” ๊ฐœ๋ฐœ ๋ชจ๋“œ์˜ debug์ค‘์—๋งŒ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.
Initializer lists์—๋Š” ์ด๋ ‡๊ฒŒ debug์ค‘์—๋งŒ ์‹คํ–‰๋˜๋Š” assert๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

class FirstTwoLetters {
  final String letterOne;
  final String letterTwo;

  FirstTwoLetters(String word)
      : assert(word.length >= 2),
        letterOne = word[0],
        letterTwo = word[1];
}
  • ์ปดํ“จํ„ฐ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

code example

class FirstTwoLetters {
  final String letterOne;
  final String letterTwo;

  // Create a constructor with an initializer list here:
  FirstTwoLetters(String word)
    ...
}

solution

class FirstTwoLetters {
  final String letterOne;
  final String letterTwo;

// Create a constructor with an initializer list here:
  FirstTwoLetters(String word)
      : assert(word.length >= 2),
        letterOne = word[0],
        letterTwo = word[1];
}

์ฐธ๊ณ  ์‚ฌ์ดํŠธ: https://dart.dev/codelabs/dart-cheatsheet#initializer-lists

 
728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

about using this in a constructor

this.propertyName ์€ ์งง๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ constructor์˜ values์— ๊ฐ’์„ ํ• ๋‹นํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

class MyColor {
  int red;
  int green;
  int blue;

  MyColor(this.red, this.green, this.blue);
}

final color = MyColor(80, 80, 128);

์ œ์‹œ๋œ ์ฝ”๋“œ์—์„œ this.์€ class ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋„ฃ๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ named parameters์—์„œ๋„ ์‚ฌ์šฉ๋œ๋‹ค.

using this in named parameters

class MyColor {
  ...

  MyColor({required this.red, required this.green, required this.blue});
}

final color = MyColor(red: 80, green: 80, blue: 80);

์œ„ ์ฝ”๋“œ๋Š” MyColor์˜ parameters์— this๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ด๋‹ค.
์—ฌ๊ธฐ์—๋Š” required๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ๋Š”๋ฐ, int values๋Š” null์ด ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • ๋งŒ์•ฝ, ๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” parameter๋ผ๋ฉด required๋ฅผ ์ƒ๋žตํ•ด๋„ ๋œ๋‹ค.

Question

MyColor([this.red = 0, this.green = 0, this.blue = 0]);
// or
MyColor({this.red = 0, this.green = 0, this.blue = 0});

์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ๋Š” ๋ฆฌ์ŠคํŠธ?
๋‘ ๋ฒˆ์งธ ์ฝ”๋“œ๋Š” property names๊ฐ€ parameters๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ.


Code example

class MyClass {
  final int anInt;
  final String aString;
  final double aDouble;
  
  // Create a constructor here.
}

Solution

class MyClass {
  final int anInt;
  final String aString;
  final double aDouble;
  
  MyClass(this.anInt, this.aString, this.aDouble);
}

์ฐธ๊ณ  ์‚ฌ์ดํŠธ: https://dart.dev/codelabs/dart-cheatsheet#initializer-lists

728x90
๋ฐ˜์‘ํ˜•

'๐Ÿ’ง flutter' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Flutter] FractionallySizedBox class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] FittedBox Class ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] CustomSingleChildLayout ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] Initializer lists ์ •๋ฆฌ  (0) 2022.07.06
[Flutter] Exceptions ์ด๋ž€!?  (0) 2022.07.06
728x90
๋ฐ˜์‘ํ˜•

Exceptions

์˜๋ฏธ: exceptions๋Š” ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์ผ์ด ๋ฐœ์ƒํ–ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ค๋ฅ˜์ด๋‹ค.
๊ธฐ๋Šฅ: dart code๋Š” exceptions๋ฅผ throw ํ•˜๊ณ  catchํ•  ์ˆ˜ ์žˆ๋‹ค.
ํŠน์ง•: dart๋Š” exception๊ณผ error types๋ฅผ ์ œ๊ณตํ•˜์ง€๋งŒ, null์ด ์•„๋‹Œ ๊ฐ์ฒด๋งŒ throw ํ•  ์ˆ˜ ์žˆ๋‹ค.

exceptions๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์•Œ์•„์•ผ ํ•˜๋Š” ์šฉ์–ด

throw

exception๋ฐœ์ƒ ์‹œ ๊ฐ์ฒด ๋˜๋Š” ์ฝ”๋“œ ๋“ฑ์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
//์ผ๋‹จ ์—๋Ÿฌ๋ฅผ ๋˜์ ธ๋ณธ๋‹ค๊ณ  ์ƒ๊ฐ

throw ์‚ฌ์šฉ ์ฝ”๋“œ ์˜ˆ)

//example 1
throw FormatException('Expected at least 1 section');
//example 2
throw 'Out of llamas!';

example 1 ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•œ๋‹ค.

  • ์—๋Ÿฌ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ด๋Š” implemeter ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ถŒ์žฅ.
    why?
    implemeter์— ๋‹ค์–‘ํ•œ exception๊ณผ error์˜ ์ข…๋ฅ˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ.

catch

catch๋Š” ๋‹ค์–‘ํ•œ exception์ƒํ™ฉ์„ ์ œ์‹œํ•ด์„œ ํŠน์ • ์ƒํ™ฉ์˜ ์—๋Ÿฌ ์ƒํ™ฉ์„ ์ œ์–ดํ•œ๋‹ค.
-> ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š” ํ•œ ์˜ˆ์™ธ๊ฐ€ ์ „ํŒŒ๋˜์ง€ ์•Š๋Š”๋‹ค.
//exception์„ catch ํ•œ๋‹ค๊ณ  ์ดํ•ดํ•จ

catch๋ฌธ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์“ฐ๋ฉด ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์—๋Ÿฌ ์ƒํ™ฉ ์ œ์–ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์˜ค๋ฅ˜ ์ƒํ™ฉ ์•ž์— on์„ ์“ฐ๊ฑฐ๋‚˜, catch๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

<์˜ˆ์‹œ์ฝ”๋“œ1>

try {
  breedMoreLlamas();
} on OutOfLlamasException {
  buyMoreLlamas();
}
//OutofLiamasException์ด๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๋ฉด buyMoreLlamas()์‹คํ–‰.

<์˜ˆ์‹œ์ฝ”๋“œ2>

try {
  breedMoreLlamas();
} on OutOfLlamasException {
  // ์ง€์ •ํ•ด๋‘” ํŠน์ • ์˜ค๋ฅ˜ ์ƒํ™ฉ(OutOfLlamas๋ผ๋Š” ์˜ค๋ฅ˜ ์ƒํ™ฉ) ์ฒ˜๋ฆฌ
  buyMoreLlamas();
} on Exception catch (e) {
  // exception ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•œ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ 
  // (์œ„์— ์„ ์–ธํ•œ OutOfLlamasException๋Š” ์ œ์™ธ)
  print('Unknown exception: $e');
} catch (e) {
  // ํƒ€์ž…์ด ํŠน์ •ํ•˜๊ฒŒ ์ง€์ •๋˜์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ
  print('Something really unknown: $e');
}
//ํ•œ ๊ฒฝ์šฐ์”ฉ ์œ„์—์„œ๋ถ€ํ„ฐ ๊ฑธ๋Ÿฌ์„œ ๋‚ด๋ ค์˜จ๋‹ค๊ณ  ์ƒ๊ฐ

rethrow

rethrow๋Š” ์˜ค๋ฅ˜๋ฅผ ์žฌ์ „ํŒŒํ•œ๋‹ค. ํ•จ์ˆ˜์˜ ์˜ค๋ฅ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์žฌ์ „ํŒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์˜ˆ์™ธ๋ฅผ ์™„์ „ํžˆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

<์˜ˆ์‹œ์ฝ”๋“œ>

void misbehave() {
  try {
    dynamic foo = true;
    print(foo++); // ๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค๋ฉด
  } catch (e) { // ์—ฌ๊ธฐ์„œ ์˜ค๋ฅ˜๋ฅผ catchํ•˜๊ณ ,
    print('misbehave() partially handled ${e.runtimeType}.');
    rethrow; // callํ•œ ํ•จ์ˆ˜(ํ•ด๋‹น ์˜ˆ์‹œ์—์„œ๋Š” mainํ•จ์ˆ˜)๊ฐ€ ์ด ์˜ค๋ฅ˜ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์žฌ์ „ํŒŒ
  }
}

void main() {
  try {
    misbehave(); // rethrow๋กœ ์œ„์—์„œ ์ฐพ์€ ์—๋Ÿฌ๊ฐ€ ์žฌ์ „ํŒŒ๋˜์–ด ๋ฐœ์ƒ.
  } catch (e) { // ์—๋Ÿฌ๋ฅผ catch
    print('main() finished handling ${e.runtimeType}.');
  }
}

finally

finally๋Š” ์˜ˆ์™ธ ๋ฐœ์ƒ์˜ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„ ์—†์ด ์ผ๋ถ€ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

finally๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์ƒํ™ฉ์œผ๋กœ ๋‚˜๋‰œ๋‹ค.

1. catch๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ

์—๋Ÿฌ ์ƒํ™ฉ์— ์•Œ๋งž๋Š” catch๋ฌธ์ด ์žˆ์œผ๋ฉด, catch๋ฌธ์„ ๋จผ์ € ์‹คํ–‰ํ•˜๊ณ  finally ์‹คํ–‰
์‹คํ–‰ ์ˆœ์„œ: catch๋ฌธ -> finally

<์˜ˆ์‹œ์ฝ”๋“œ>

try {
  breedMoreLlamas();
} catch (e) {
  print('Error: $e'); // ์—๋Ÿฌ๋ฅผ ๋จผ์ € ์ œ์–ดํ•œ๋‹ค.
} finally {
  cleanLlamaStalls(); // ๊ทธ ๋‹ค์Œ, ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
}

2. catch๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ

์—๋Ÿฌ ์ƒํ™ฉ์— ์•Œ๋งž๋Š” catch๋ฌธ์ด ์—†์œผ๋ฉด exception์ด ์žˆ์–ด๋„ finally๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
์‹คํ–‰ ์ˆœ์„œ: finally -> exception

<์˜ˆ์‹œ์ฝ”๋“œ>

try {
  breedMoreLlamas();
} finally {
    // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ์•„๋ž˜ ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜ ์‹คํ–‰
  cleanLlamaStalls();
  //catch๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ์žฌ์ „ํŒŒ ๋œ๋‹ค.
}

exception code example

typedef VoidFunction = void Function();

class ExceptionWithMessage {
  final String message;
  const ExceptionWithMessage(this.message);
}

// Call logException to log an exception, and doneLogging when finished.
abstract class Logger {
  void logException(Type t, [String? msg]);
  void doneLogging();
}

void tryFunction(VoidFunction untrustworthy, Logger logger) {
  // Invoking this method might cause an exception. Catch and handle
  // them using try-on-catch-finally.
  untrustworthy();
}

solution code

typedef VoidFunction = void Function();

class ExceptionWithMessage {
  final String message;
  const ExceptionWithMessage(this.message);
}
// Call logException to log an exception, and doneLogging when finished.
abstract class Logger {
  void logException(Type t, [String? msg]);
  void doneLogging();
}

void tryFunction(VoidFunction untrustworthy, Logger logger) {
// Invoking this method might cause an exception. Catch and handle
    // them using try-on-catch-finally.
  try {
    untrustworthy();
  } on ExceptionWithMessage catch (e) {
    logger.logException(e.runtimeType, e.message);
  } on Exception {
    logger.logException(Exception);
  } finally {
    logger.doneLogging();
  }
}

์ฐธ๊ณ  ์‚ฌ์ดํŠธ1: https://m.blog.naver.com/mingdyuo/221803704762
์ฐธ๊ณ  ์‚ฌ์ดํŠธ2: https://dart.dev/codelabs/dart-cheatsheet#initializer-lists

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

728x90

git checkout์„ ํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋– ์„œ ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด ๋˜์—ˆ๋‹ค. 

ํ˜„์žฌ vscode๋ฅผ ์‚ฌ์šฉ ์ค‘์ธ๋ฐ, ํด๋” ์˜†์— ! ์ด๋Ÿฐ ํ‘œ์‹œ๊ฐ€ ์ƒ๊ฒผ๋‹ค. ์•„๋ฌด๋ฆฌ ์—๋Ÿฌ๋ฅผ ์ฐพ์•„๋ณด์•„๋„ ์—๋Ÿฌ๋Š” ๋ณด์ด์ง€ ์•Š๊ณ , ๋นŒ๋“œ๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค.

 

๊ทธ๋ž˜์„œ ์ผ๋‹จ git status ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋””์—์„œ ์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์•˜๋‹ค.

$git status

 

๊ทธ ๊ฒฐ๊ณผ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์•„๋ฌด๋ž˜๋„ ์ˆ˜์ •ํ•œ ๊ฒƒ์„ ์ œ๋Œ€๋กœ ์ €์žฅ ๋˜๋Š” push ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ merge๋ฅผ ํ•ด์„œ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

์ด๋ ‡๊ฒŒ git status๋ฅผ ํ†ตํ•ด ์—๋Ÿฌ๊ฐ€ ๋‚œ ๋ถ€๋ถ„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์—๋Ÿฌ์— ๋‚˜์˜จ๋Œ€๋กœ ๊ฐ ํŒŒ์ผ์„ ํ•˜๋‚˜์”ฉ git addํ•˜์—ฌ ์ˆ˜์ •๋œ ๊ฒƒ์„ ์ €์žฅํ•˜์˜€๋‹ค.

 

์ž˜ ๋ณด์ผ์ง€ ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜, ๊ธฐ์กด์— home.dart ํŒŒ์ผ์ด ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ, add๋ฅผ ํ•˜๊ณ  git status๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ ํ•ด๋‹น ํŒŒ์ผ์ด ์‚ฌ๋ผ์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

git add {commit์ด ํ•„์š”ํ•œ ํŒŒ์ผ ๊ฒฝ๋กœ}

(ex. $git add lib/screen/home.dart)

 

์ด๋ ‡๊ฒŒ git status๋ฅผ ํ†ตํ•ด add๊ฐ€ ํ•„์š”ํ•œ ํŒŒ์ผ์„ ์ฐพ๊ณ  git add๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ํŒŒ์ผ์„ ๋ชจ๋‘ ํ•ด๊ฒฐํ•˜๊ณ  ๋‚œ ๋’ค git checkout์„ ํ–ˆ๋”๋‹ˆ ๋“œ๋””์–ด ๋๋‹ค.

 

git error๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ์—๋Š” ๋จผ์ € git status๋ฅผ ํ†ตํ•ด ๊ผญ ๋จผ์ € ํ™•์ธํ•ด๋ณด๋Š” ์Šต๊ด€์„ ๊ฐ€์ ธ์•ผ๊ฒ ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•

'๐Ÿˆโ€โฌ› git' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Git] ๋ธŒ๋žœ์น˜ ์ „๋žต (Git, GitHub, GitLab)  (0) 2024.07.27
git history ์‚ญ์ œํ•˜๊ธฐ  (0) 2022.10.04

+ Recent posts