1. 이미지를 로컬 장치에 저장해보자.
서버 DB를 사용하지 않을거라면 모바일에 db를 생성하여 데이터를 저장/불러올 수 있다.
flutter pub add sqflite 이거는 sql 구문 날리는식으로 db를 사용 할 수 있고 (해당 강의에서는 이 패키지 사용)
아래 패키지는 간단하게 key, value 식으로 이용 할 수 있는 것 같다.
https://pub.dev/packages/shared_preferences
shared_preferences | Flutter Package
Flutter plugin for reading and writing simple key-value pairs. Wraps NSUserDefaults on iOS and SharedPreferences on Android.
pub.dev
그리고 이미지가 저장된 위치를 알아 내기 위해 2가지 패키지를 다운 받아보자.
flutter pub add path_provider (폴더 경로 알아 낼 수 있음)
flutter pub add path (최종 파일 경로 알 수 있음)
각 시스템에 따라 저장된 위치가 다르기 때문에 위 패키지를 이용해서 알아 낼 수 있다.
- user_places.dart
import 'dart:io';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:path_provider/path_provider.dart' as syspaths;
import 'package:path/path.dart' as path;
import 'package:favorite_places/models/place.dart';
class UserPlacesNotifier extends StateNotifier<List<Place>> {
UserPlacesNotifier() : super(const []);
void addPlace(String title, File image, PlaceLocation location) async {
final appDir = await syspaths.getApplicationDocumentsDirectory();
final filename = path.basename(image.path);
final copiedImage = await image.copy('${appDir.path}/$filename');
final newPlace =
Place(title: title, image: copiedImage, location: location);
state = [newPlace, ...state];
}
}
final userPlacesProvider =
StateNotifierProvider<UserPlacesNotifier, List<Place>>(
(ref) => UserPlacesNotifier(),
);
1) 앱의 폴더명과 image의 최종 파일명을 알아 내어 최종적으로 copiedImage에 복사를 해낸 파일을 담아 낼 수 있다.
'코딩강의 > favorite_places(플러터-유데미)' 카테고리의 다른 글
~262. Using a FutureBuilder for Loading Data (1) | 2023.11.22 |
---|---|
~257. Using the Map Screen in the "Add Place" Form (0) | 2023.11.21 |
~255. Displaying the Picked Place on a Dynamic Map (0) | 2023.11.21 |
~252. Outputting the Location Data (1) | 2023.11.21 |
~250. Displaying a Location Preview Map Snapshot via Google (0) | 2023.11.20 |