Search
๐Ÿฅ

Flutter API Request with Provider pattern

์ƒ์„ฑ์ผ
2021/05/11 12:33
ํƒœ๊ทธ
Flutter
์†์„ฑ

Dependency

flutter pub add http, provider
Bash
๋ณต์‚ฌ

Entity ์ถ”๊ฐ€

Response ๋ฅผ ํ†ตํ•ด ๋ฐ›์•„์˜ฌ Entity๋ฅผ ๋ฏธ๋ฆฌ ์„ ์–ธํ•œ๋‹ค

Promotion.dart

class Promotion { final String brandName; final String brandLogoURL; final int boltPrice; final int boltEffectiveness; final int discountRate; final int priceMax; final int discountMax; final String description; Promotion({ this.brandName, this.brandLogoURL, this.boltPrice, this.boltEffectiveness, this.discountRate, this.priceMax, this.discountMax, this.description }); factory Promotion.fromJson(Map<String, dynamic> json) { return Promotion( brandName: json['id'], brandLogoURL: json["brandLogoURL"], boltPrice: json["boltPrice"], boltEffectiveness: json['boltEffectiveness'], discountRate: json['discountRate'], priceMax: json['priceMax'], discountMax: json['discountMax'], description: json['description'], ); } }
Dart
๋ณต์‚ฌ

Provider ๊ตฌํ˜„

๊ฐ€์žฅ ํŠธ๋ Œ๋“œ(?)์ธ Provider ํŒจํ„ด์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.
์ด ๋ถ€๋ถ„์—์„œ ์ด์Šˆ๊ฐ€ ์žˆ๋˜ ๋ถ€๋ถ„์€ ๋ณดํ†ต ์ž๋™ import ๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ http ๋ชจ๋“ˆ์˜ ๊ฒฝ์šฐ ์ž๋™ import ๋˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ
import 'package:http/http.dart' as http;
Dart
๋ณต์‚ฌ
์ง์ ‘ import ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

PromotionProvider.dart

import 'dart:convert'; import 'package:chai_booster/Entity/Promotion.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; class PromotionProvider with ChangeNotifier { List<Promotion> _promotions = []; List<Promotion> getPromotionList() { _fetchPromotion(); return _promotions; } // Action void _fetchPromotion() async { final response = await http.get(Uri.parse('http://localhost:5001/chai-booster/asia-northeast3/api/booster/v1/promotions')); final List<Promotion> parsedResposne = jsonDecode(response.body). map<Promotion>((json) => Promotion.fromJson(json)).toList(); _promotions.clear(); _promotions.addAll(parsedResposne); notifyListeners(); } }
Dart
๋ณต์‚ฌ

HomePage.dart

import 'package:chai_booster/Pages/Tabs/BrandAlarmSettingPage.dart'; import 'package:chai_booster/Pages/Tabs/MorePage.dart'; import 'package:chai_booster/Pages/Tabs/PromotionsPage.dart'; import 'package:chai_booster/Providers/Auth/AuthProvider.dart'; import 'package:chai_booster/Providers/Promotion/PromotionProvider.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:provider/provider.dart'; class HomePage extends StatefulWidget { HomePage({ Key key, this.title }) : super(key: key); final String title; _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { AuthProvider authProvider; int _currentIndex = 0; Widget build(BuildContext context) { authProvider = Provider.of<AuthProvider>(context); return Scaffold( body: IndexedStack( index: _currentIndex, children: [ for (final tabItem in TabNavigationItem.items) tabItem.page, ], ), bottomNavigationBar: BottomNavigationBar( currentIndex: _currentIndex, onTap: (int index) => setState(() => _currentIndex = index), items: [ for (final tabItem in TabNavigationItem.items) BottomNavigationBarItem( icon: tabItem.icon, label: tabItem.title, ) ], ), ); } } class TabNavigationItem { final Widget page; final String title; final Icon icon; TabNavigationItem({ this.page, this.title, this.icon, }); static List<TabNavigationItem> get items => [ TabNavigationItem( page: ChangeNotifierProvider<PromotionProvider>( create: (context) => PromotionProvider(), child: PromotionPage(), ), icon: Icon(Icons.money), title: "ํ˜œํƒ", ), TabNavigationItem( page: BrandAlarmSettingPage(), icon: Icon(Icons.alarm), title: "์•Œ๋ฆผ", ), TabNavigationItem( page: MorePage(), icon: Icon(Icons.more), title: "๋”๋ณด๊ธฐ", ), ]; }
Dart
๋ณต์‚ฌ
์ฝ”๋“œ๊ฐ€ ๊ธธ๋‹ค. ํ•˜์ง€๋งŒ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ์•„๋ž˜ ๋ถ€๋ถ„์ด๋‹ค.
static List<TabNavigationItem> get items => [ TabNavigationItem( page: ChangeNotifierProvider<PromotionProvider>( create: (context) => PromotionProvider(), child: PromotionPage(), ), icon: Icon(Icons.money), title: "ํ˜œํƒ", ),
Dart
๋ณต์‚ฌ
PromotionPage ์—์„œ state ๋ฅผ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๊ธฐ ๋•Œ๋ฌธ์—, PromotionPage ๋ฅผ ChangeNotifierProvider๋กœ ๊ฐ์‹ธ์ค€๋‹ค.

PromotionPage.dart

import 'package:chai_booster/Entity/Promotion.dart'; import 'package:chai_booster/Pages/HomePage.dart'; import 'package:chai_booster/Providers/Promotion/PromotionProvider.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class PromotionPage extends StatefulWidget { PromotionPage({ Key key, this.title }) : super(key: key); final String title; _PromotionPageState createState() => _PromotionPageState(); } class _PromotionPageState extends State<PromotionPage> { Future<Promotion> promotions; Widget build(BuildContext context) { final promotionProvider = Provider.of<PromotionProvider>(context); promotionProvider.getPromotionList(); return Scaffold( appBar: AppBar( title: Text('ํ˜œํƒ'), centerTitle: false, ), body: Center( child: SizedBox( child: ElevatedButton( onPressed: () => { }, child: Text('ํ˜œํƒ ๋ชฉ๋ก'), ), width: double.infinity, height: 44, ), ), ); } }
Dart
๋ณต์‚ฌ
์‹ค์ œ ์šด์˜ํ•  ์„œ๋น„์Šค์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„์€
promotionProvider.getPromotionList();
Dart
๋ณต์‚ฌ
์™œ?
build ํ•จ์ˆ˜ ์•ˆ์—์žˆ๋‹ค๋Š” ๋œป์€, widget ์ด ๊ทธ๋ ค์งˆ๋•Œ๋งˆ๋‹ค API request๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ถˆํ•„์š”ํ•œ ๊ณผ๋„ํ•œ ์š”์ฒญ์ด ๋ฐœ์ƒํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ผ ์•ฑ์ด ๋ฒ„๋ฒ…์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

API Request ๋ฅผ ๋ฐœ์ƒํ•˜๋Š” ์‹œ์ , ๋กœ์ง

isLoaded ๋ผ๋Š” ๋ณ€์ˆ˜... ํ ... ์ฐ์ฐํ•˜๋‹ค. reload ํ•  ํƒ€์ด๋ฐ์ด ๋ช…ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ