๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”ฅ/Project๐Ÿ‘จ‍๐Ÿ’ป

[Flutter] SQLite ์—ฐ๋™ํ•˜๋‹ค ์ƒ๊ธด ์ผ (feat. await async)

by narang111 2022. 9. 12.

The await expression can only be used in an async function.  

Try marking the function body with either 'async' or 'async*'.

์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค.

์–ด์จŒ๋“  ํ•ด๊ฒฐํ•จ

 

 

๋ถ„๋ช…!!! ์ •์‹ ๋ฌธํ—Œ์„ ๋ณด๊ณ  ํ–ˆ๋Š”๋ฐ ๋ง์ด์ง€....์ง„์งœ flutter๋Š” ์•Œ๋‹ค๊ฐ€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค

์•„๋‹ˆ ๋ชจ๋ฅด๋‹ค๊ฐ€ ๋ชจ๋ฅด๊ฒ ์Œ

https://flutter-ko.dev/docs/cookbook/persistence/sqlite

 

SQLite์— ๋ฐ์ดํ„ฐ ์ €์žฅํ•˜๊ธฐ

๋กœ์ปฌ ๋””๋ฐ”์ด์Šค์— ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ฟผ๋ฆฌ๋ฅผ ์š”์ฒญํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ๋กœ์ปฌ ํŒŒ์ผ์ด๋‚˜ ํ‚ค-๊ฐ’ ์ €์žฅ์†Œ ๋Œ€์‹  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ์‚ฌ์šฉํ•ด๋ณด์„ธ์š”. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๋‹ค๋ฅธ ๋กœ์ปฌ ์†”๋ฃจ์…˜๋ณด๋‹ค ๋” ๋น ๋ฅธ

flutter-ko.dev

 

 

โœ… ํ•ด๊ฒฐํ•œ ๋ฐฉ๋ฒ•!

https://www.youtube.com/watch?v=noi6aYsP7Go 

 ์ผ๋‹จ ์ด ๋ถ„์ด ํ•˜์‹œ๋Š” ๋Œ€๋กœ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผํ•œ๋‹ค.  ์ด ์Œค์ด ํ•˜๋Š” ๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผํ•ด๋„ ์•ˆ ๋๋Š”๋ฐ ์•„๋ž˜ ํ•ด๊ฒฐํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.
import 'dart:io';


main(){
  WidgetsFlutterBinding.ensureInitialized();
  runApp(HomePage());
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  TextEditingController _eventController = TextEditingController();
  String name="";
  @override
  Widget build(BuildContext context){
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("challenge"),
        ),
        body: Center(
          child: FutureBuilder<List<Challenge>>(
            future: DatabaseHelper.instance.getChallenge(),
            builder: (BuildContext context,
                AsyncSnapshot<List<Challenge>> snapshot){
              if(!snapshot.hasData){
                return Center(child:Text('Loading'));
              }
              return snapshot.data!.isEmpty
                ? Center(child:Text(''))
                : ListView(
                  children: snapshot.data!.map((challenge){
                    return Center(
                      child:ListTile(
                        title:Text(challenge.challenge),
                        
                      ),
                    );
                  }).toList(),
                );
            }),
        ),
        ..์ฝ”๋“œ์ƒ๋žต..
    );
  }
}

class Challenge{
  final int? id;
  final String date;
  final String challenge;

  Challenge({this.id, required this.date, required this.challenge});

  factory Challenge.fromMap(Map<String, dynamic> json) => new Challenge(
      id       : json['id'],
      

  Map<String, dynamic> toMap(){
    return {
      'id' : id,
      
    };
  }
}

class DatabaseHelper{
  DatabaseHelper._privateConstructor();
  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();

  static Database? _database;
  Future<Database> get database async => _database ??=await _initDatabase();

  Future<Database> _initDatabase() async{
 // โŒโŒโŒโŒ
    // Directory documentsDirectory = await getApplicationDocumentsDirectory();
    // String path = join(documentsDirectory.path, '๋””๋น„์ด๋ฆ„.db');
 // ์ •์‹๋ฌธํ—Œ ์ฐธ๊ณ    
    String path = join(await getDatabasesPath(), '๋””๋น„์ด๋ฆ„.db');
    return await openDatabase(
      path,
      version: 1,
      onCreate: _onCreate,
    );
  }
  Future _onCreate(Database db, int version) async{
    await db.execute('''
      CREATE TABLE my_challenge(
        id INTEGER PRIMARY_KEY,
       
      )
    ''');
  }

  Future<List<Challenge>> getChallenge() async {
    Database db = await instance.database;
    var challenges = await db.query('๋””๋น„์ด๋ฆ„', orderBy: 'date');
    List<Challenge> challengeList = challenges.isNotEmpty
        ? challenges.map((c)=>Challenge.fromMap(c)).toList()
        :[];
    return challengeList;
  }

  Future<int> add(Challenge challenge) async{
    Database db = await instance.database;
    return await db.insert('๋””๋น„์ด๋ฆ„', challenge.toMap());
  }


}

 

๊ทธ๋Œ€๋กœ ์น˜๋ฉด ๋˜ database๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” error ๋กœ๊ทธ๊ฐ€ ๋‚˜์˜จ๋‹ค.

๊ทธ๋ž˜์„œ N๋ฒˆ์˜ ์‹œ๋„ ๋์—...๐Ÿ˜ฅ path ๋ถ€๋ถ„์ด ์ž˜๋ชป ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์ด ๋ถ€๋ถ„๋งŒ ์ •์‹ ๋ฌธํ—Œ์—์„œ ๊ฐ€์ ธ์™€์ฃผ์—ˆ๋‹ค.

์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๋ถ€๋ถ„์€ ์ฃผ์„์ฒ˜๋ฆฌ ํ•ด๋‘์—ˆ๋‹ค.

 

getDatabasesPath๋Š” sqflite์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ์งœ๋Š” ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋‹ค.