고원빈

[frontend] sqltie 생성

......@@ -93,4 +93,7 @@ appbar 관련 디자인은 추후 구현 예정
### 2021-05-29
+ 약병 리스트 ui 변경
+ DashBoard 새로고침
\ No newline at end of file
+ DashBoard 새로고침
### 2021-05-30
+ Sqlite 생성
\ No newline at end of file
......
......@@ -6,6 +6,8 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
import '../models/Bottle.dart';
import '../DashBoard.dart';
import '../../utils/user_secure_stoarge.dart';
import '../../utils/DBHelper.dart';
import '../models/UserBottle.dart';
class BottleList extends StatefulWidget {
BottleList({Key key}) : super(key: key);
......@@ -19,13 +21,14 @@ class _BottleListState extends State<BottleList> {
Future<String> getBottleList() async {
String hubid = await UserSecureStorage.getHubId();
String usertoken = await UserSecureStorage.getUserToken();
var provider = DBHelper();
http.Response response = await http.get(
Uri.encodeFull(
DotEnv().env['SERVER_URL'] + 'bottle/hub/' + hubid.toString()),
headers: {"authorization": usertoken},
);
print(response.body);
print(1);
if (_bottleList.length != 0) {
_bottleList.clear();
}
......@@ -37,6 +40,20 @@ class _BottleListState extends State<BottleList> {
Map<String, dynamic> map = values[i];
_bottleList.add(Bottle.fromJson(map));
}
for (int i = 0; i < _bottleList.length; i++) {
UserBottle temp = new UserBottle();
temp.bottleId = _bottleList[i].bottleId;
temp.bottleName = _bottleList[i].bottleId.toString();
provider.createData(temp);
}
List<UserBottle> _userbottleList = new List<UserBottle>();
_userbottleList = await provider.getAllBottle();
for (int i = 0; i < _userbottleList.length; i++) {
print(_userbottleList[i].bottleId);
print(12345678);
}
print(provider.getAllBottle());
return "GET";
} else if (response.statusCode == 404) {
return "Not Found";
......
class UserBottle {
String bottleId;
int bottleId;
String bottleName;
UserBottle({this.bottleId, this.bottleName});
factory UserBottle.fromJson(Map<String, dynamic> parsedJson) {
return UserBottle(
bottleId: parsedJson['bottleId'],
bottleName: parsedJson['bottleName'],
);
}
Map<String, dynamic> toJson() =>
{"bottleId": bottleId, "bottleName": bottleName};
}
......
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import '../screens/models/UserBottle.dart';
final String tableName = 'medicinename';
class DBHelper {
DBHelper._();
static final DBHelper _db = DBHelper._();
factory DBHelper() => _db;
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
_database = await initDB();
return _database;
}
initDB() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, 'medicinename.db');
return await openDatabase(
path,
version: 1,
onCreate: (Database db, int version) async {
await db.execute('''
CREATE TABLE $tableName
(bottleId INTEGER PRIMARY KEY,
bottleName TEXT)
''');
},
);
}
createData(UserBottle bottle) async {
final db = await database;
var res = await db.insert(tableName, bottle.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace);
return res;
}
getBottle(int bottleId) async {
final db = await database;
var res =
await db.query(tableName, where: 'bottleId=?', whereArgs: [bottleId]);
return res.isNotEmpty ? UserBottle.fromJson(res.first) : Null;
}
Future<List<UserBottle>> getAllBottle() async {
final db = await database;
var res = await db.query(tableName);
List<UserBottle> list =
res.isNotEmpty ? res.map((c) => UserBottle.fromJson(c)).toList() : [];
return list;
}
updateBottle(UserBottle bottle) async {
final db = await database;
var res = db.update(tableName, bottle.toJson(),
where: 'bottleId=?', whereArgs: [bottle.bottleId]);
return res;
}
}