๐Ÿฆ‹Flutter - ๋‹ค์Œ ๋‹จ๊ณ„ ํƒ์ƒ‰ - ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ

9411 ๋‹จ์–ด flutterprogrammingdartproductivity

If you are using a dynamic class for routing in Flutter, you may be concerned about errors. How about a static error page (404) when routing fails so the user knows what's going on?!



์ด๊ฒƒ์€ ๋‚ด ์‹œ๋ฆฌ์ฆˆ์˜ 4๋ถ€ ์ค‘ 3๋ถ€์ž…๋‹ˆ๋‹ค: ๐Ÿฆ‹Flutter - ๋‹ค์Œ ๋ ˆ๋ฒจ ํƒ์ƒ‰.
์•„์ง ์ฝ์ง€ ์•Š์œผ์…จ๋‹ค๋ฉด ์ฝ์–ด๋ณด์‹œ๊ธธ ๊ถŒํ•ฉ๋‹ˆ๋‹ค.

์ฝ˜ํ…์ธ 



  • Error handling

  • 1. Create an error page
  • 2. Link to the error page

  • Individualize the error page

  • ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ



    ์•ฑ ๋‚ด์—์„œ ํƒ์ƒ‰ํ•˜๋Š” ๋™์•ˆ ์˜ค๋ฅ˜๋ฅผ ํฌ์ฐฉํ•˜๊ธฐ ์œ„ํ•ด ์ง€๋‚œ ๊ฒŒ์‹œ๋ฌผ์— ์˜ค๋ฅ˜ ์ฟผ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์‚ฌ์šฉ์ž๋ฅผ ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋””๋ ‰์…˜ํ•˜๋ ค๋ฉด ๊ฐ„๋‹จํ•œ 2๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

    1. ์˜ค๋ฅ˜ ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ



    ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๋ณ„๋„์˜ ํด๋ž˜์Šค์—์„œ ์ˆ˜ํ–‰ํ•˜๊ฑฐ๋‚˜ ์ด์ „์— ๋งŒ๋“  ํด๋ž˜์ŠคRouteGenerator์—์„œ ์ง์ ‘ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๊ฐ€ ๋‹จ์ˆœํ•˜๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ ์˜ต์…˜์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

    ์˜ค๋ฅ˜ ํŽ˜์ด์ง€์˜ ๊ฒฝ์šฐ ์ด์ œ ๊ฐ์ฒด_errorRoute๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋‚ด๋ถ€ ์ „์šฉ ํ•จ์ˆ˜Route<dynamic>๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋œ ํŽ˜์ด์ง€์—์„œ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์„ค์ •ํ•˜๋Š” ์Šค์บํด๋“œ ๊ฐœ์ฒด๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      static Route<dynamic> _errorRoute() {
        return MaterialPageRoute(builder: (_) {
          return Scaffold(
            appBar: AppBar(
              title: Text('Error'),
            ),
            body: Center(
              child: Text('ERROR'),
            ),
          );
        });
      }
    

    2. ์˜ค๋ฅ˜ ํŽ˜์ด์ง€ ๋งํฌ



    ์ด์ œ ๋ชจ๋“  ์˜ค๋ฅ˜ ์‚ฌ๋ก€์—์„œ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋””๋ ‰์…˜ํ•˜๋ ค๋ฉด ๋ชจ๋“  ์˜ค๋ฅ˜์—์„œ ํ•จ์ˆ˜_errorRoute()๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์ „์— ๋งŒ๋“  switch-case ๋ธ”๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค.

    class RouteGenerator {
      static Route<dynamic> generateRoute(RouteSettings settings) {
    
        final args = settings.arguments;
    
        switch (settings.name) {      
          case '/page2':
            if (args is int) {
              return MaterialPageRoute(
                  builder: (_) =>
                      IntroductionView(arguments: args));
            }
            return _errorRoute();
          default:
            return _errorRoute();
        }
      }
    

    ์˜ค๋ฅ˜ ํŽ˜์ด์ง€ ๊ฐœ๋ณ„ํ™”



    ๋ฌผ๋ก  ์˜ค๋ฅ˜ ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜ค๋ฅ˜์˜ ์›์ธ์„ ์ „๋‹ฌํ•˜์—ฌ ์˜ค๋ฅ˜๋ฅผ ๋ณด๋‹ค ๊ตฌ์ฒด์ ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      static Route<dynamic> _errorRoute({String message}) {
        return MaterialPageRoute(builder: (_) {
          return Scaffold(
            appBar: AppBar(
              title: Text('Error'),
            ),
            body: Center(
              child: Text('ERROR: ' + message),
            ),
          );
        });
      }
    

    &&

    default:
            return _errorRoute(message: 'wrong routing name');
    

    ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ์˜ˆ์ œ์—์„œ์™€ ๊ฐ™์ด ์ „๋‹ฌ๋œ ์˜ค๋ฅ˜๋ฅผ ์„ ํƒ ์‚ฌํ•ญ์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ผ๋งˆ๋‚˜ ์•„์ด๋Ÿฌ๋‹ˆํ•œ ์ผ์ด๊ฒ ์Šต๋‹ˆ๊นŒ! XD


    ๋‹ค์Œ ๊ฒŒ์‹œ๋ฌผ์„ ๋†“์น˜์ง€ ์•Š๋„๋ก
    Github ๋˜๋Š” Lucianojung.de์—์„œ ๋‚ด ์ตœ์‹  ํ”„๋กœ์ ํŠธ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

    ๋‹น์‹ ์€ ๋˜ํ•œ ์ข‹์•„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:














    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ