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์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ์ง์ ์ง๋ ๋ถ๋ถ์ด ์๋๋ค.