[flutter] shared_preferences 를 사용하여 사용자가 쓴 리뷰 목록을 저장하는 방법은 무엇인가요?
  1. 먼저 shared_preferences 패키지를 프로젝트에 추가합니다. pubspec.yaml 파일의 dependencies 섹션에 아래 코드를 추가합니다:
dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^0.5.12
  1. 패키지를 추가한 후, pub get 명령어를 사용하여 의존성을 다운로드합니다.

  2. 이제 shared_preferences를 사용하여 리뷰 목록을 저장하는 기능을 구현할 수 있습니다.

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ReviewListScreen extends StatefulWidget {
  @override
  _ReviewListScreenState createState() => _ReviewListScreenState();
}

class _ReviewListScreenState extends State<ReviewListScreen> {
  List<String> reviewList = [];

  @override
  void initState() {
    super.initState();
    getReviewList();
  }

  Future<void> getReviewList() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<String> storedReviewList = prefs.getStringList('reviewList');
    if (storedReviewList != null) {
      setState(() {
        reviewList = storedReviewList;
      });
    }
  }

  Future<void> saveReviewList() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setStringList('reviewList', reviewList);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("리뷰 목록"),
      ),
      body: ListView.builder(
        itemCount: reviewList.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text(reviewList[index]),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              TextEditingController reviewController = TextEditingController();
              return AlertDialog(
                title: Text("리뷰 작성"),
                content: TextField(
                  controller: reviewController,
                  decoration: InputDecoration(hintText: "리뷰를 입력하세요."),
                ),
                actions: [
                  FlatButton(
                    child: Text("취소"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                  FlatButton(
                    child: Text("저장"),
                    onPressed: () async {
                      setState(() {
                        reviewList.add(reviewController.text);
                      });
                      saveReviewList();
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            },
          );
        },
        child: Icon(Icons.add),
      ),
    );
  }
}

위의 예시 코드는 ReviewListScreen 클래스에 리뷰 목록을 표시하는 리스트뷰 위젯을 구현합니다. 사용자가 리뷰를 작성하고 저장하면, 해당 리뷰는 SharedPreferences를 이용하여 저장됩니다. getReviewList 메서드는 앱이 시작될 때 저장된 리뷰 목록을 불러옵니다. saveReviewList 메서드는 새로운 리뷰를 추가하고 변화된 목록을 저장합니다.

이제 ReviewListScreen을 앱의 원하는 위치에서 호출하여 리뷰 목록을 사용할 수 있습니다.