[Flutter] BottomNavigationBar 구현시 메모리 고려사항

기존 코드 import 'package:flutter/material.dart'; import 'package:meals_clone/screens/categories.dart'; // import 'package:riverpod/riverpod.dart'; class TabScreen extends StatefulWidget { const TabScreen({super.key}); @override State<TabScreen> createState() => _TabScreenState(); } class _TabScreenState extends State<TabScreen> { int _selectedPageIndex = 0; Widget categoriesScreen = const CategoriesScreen(); Widget placeholder = const Placeholder(); Widget activePage = const CategoriesScreen(); void _selectPage(int index){ setState(() { _selectedPageIndex = index; }); if(_selectedPageIndex == 0){ activePage = categoriesScreen; } else if(_selectedPageIndex == 1){ activePage = placeholder; } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Categories'), centerTitle: true, ), // body: const CategoriesScreen(), body: activePage, bottomNavigationBar: BottomNavigationBar( items: const [ BottomNavigationBarItem( icon: Icon( Icons.set_meal, size: 30, ), label: '카테고리'), BottomNavigationBarItem( icon: Icon( Icons.star, size: 30, ), label: '즐겨찾기') ], showSelectedLabels: false, // 선택된 레이블 숨기기 showUnselectedLabels: false, // 선택되지 않은 레이블 숨기기 currentIndex: _selectedPageIndex, onTap: _selectPage, ), ); } } 수정된 코드 import 'package:flutter/material.dart'; import 'package:meals_clone/screens/categories.dart'; // import 'package:riverpod/riverpod.dart'; class TabScreen extends StatefulWidget { const TabScreen({super.key}); @override State<TabScreen> createState() => _TabScreenState(); } class _TabScreenState extends State<TabScreen> { int _selectedPageIndex = 0; void _selectPage(int index) { setState(() { _selectedPageIndex = index; }); } @override Widget build(BuildContext context) { // 현재 선택된 페이지에 따라 적절한 위젯을 동적으로 선택합니다. Widget activePage = _selectedPageIndex == 0 ? const CategoriesScreen() : const Placeholder(); return Scaffold( appBar: AppBar( title: const Text('Categories'), centerTitle: true, ), body: activePage, bottomNavigationBar: BottomNavigationBar( items: const [ BottomNavigationBarItem( icon: Icon( Icons.set_meal, size: 30, ), label: '카테고리'), BottomNavigationBarItem( icon: Icon( Icons.star, size: 30, ), label: '즐겨찾기') ], showSelectedLabels: false, // 선택된 레이블 숨기기 showUnselectedLabels: false, // 선택되지 않은 레이블 숨기기 currentIndex: _selectedPageIndex, onTap: _selectPage, ), ); } } 수정된 내용 불필요한 인스턴스화 제거: categoriesScreen과 placeholder 위젯을 미리 인스턴스화할 필요가 없다. 이들은 필요할 때 생성될 수 있으며, 위와 같이 수정하면 메모리 사용량을 줄이고 성능을 향상시킬 수 있다. ...

2023년 11월 30일 · 4 분 · An Seongjin

[Flutter] 에러 내용 “MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)”

에러 발생 개발 중 다음과 같은 로그가 발생했다. MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider) Hot Restart를 해도 동일하게 에러가 출력. 그러나 동작에는 문제가 없는 상황 . . . 해결 💢 단순히 IDE와 시뮬레이터 재부팅 혹은 flutter clean Flutter 공식 이슈이기도 한 문제 ⇒ https://github.com/flutter/flutter/issues/10912

2023년 11월 14일 · 1 분 · An Seongjin