Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion how to update the BLOB type. #2

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/issue/task.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:typed_data';

import 'package:floor/floor.dart';
import 'package:flutter/services.dart';

@Entity(tableName: Task.tableName)
class Task {
Expand Down
6 changes: 6 additions & 0 deletions lib/issue/task_dao.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:typed_data';
import 'package:floor/floor.dart';
import 'package:floor_issue/issue/task.dart';
import 'package:floor_issue/main.dart';

@dao
abstract class TaskDao {
Expand All @@ -12,4 +13,9 @@ abstract class TaskDao {

@Query('UPDATE ${Task.tableName} SET data = :data WHERE id = :id')
Future<void> updateData(Uint8List data, int id);

Future<void> rawUpdateData(Uint8List data, int id) {
return database.database.rawUpdate(
'UPDATE ${Task.tableName} SET data = ? WHERE id = ?', [data, id]);
}
}
31 changes: 12 additions & 19 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ import 'dart:typed_data';

import 'package:floor_issue/issue/database.dart';
import 'package:floor_issue/issue/task.dart';
import 'package:floor_issue/issue/task_dao.dart';
import 'package:flutter/material.dart';

late final AppDatabase database;

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

final database =
await $FloorAppDatabase.databaseBuilder('flutter_database.db').build();
final dao = database.taskDao;
runApp(MyApp(
taskDao: dao,
));
database = await $FloorAppDatabase.databaseBuilder('flutter_database.db').build();
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
final TaskDao taskDao;

const MyApp({required this.taskDao, super.key});
const MyApp({super.key});

// This widget is the root of your application.
@override
Expand All @@ -30,15 +25,13 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(taskDao: taskDao, title: 'Flutter Demo Home Page'),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
final TaskDao taskDao;

const MyHomePage({super.key, required this.taskDao, required this.title});
const MyHomePage({super.key, required this.title});

final String title;

Expand All @@ -47,6 +40,7 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
late final taskDao = database.taskDao;
final int taskId = 1;

@override
Expand All @@ -64,9 +58,9 @@ class _MyHomePageState extends State<MyHomePage> {
String source = 'hello';
List<int> list = utf8.encode(source);
Uint8List data = Uint8List.fromList(list);
await widget.taskDao.insertTask(Task(taskId, data));
await taskDao.insertTask(Task(taskId, data));

widget.taskDao.findTaskById(taskId).then((value) {
taskDao.findTaskById(taskId).then((value) {
print(
'insertTask ${value?.id}, ${utf8.decode(value?.data ?? Uint8List(0))}');
});
Expand All @@ -79,9 +73,8 @@ class _MyHomePageState extends State<MyHomePage> {
String source = 'new_hello';
List<int> list = utf8.encode(source);
Uint8List data = Uint8List.fromList(list);
await widget.taskDao.updateData(data, taskId);

widget.taskDao.findTaskById(taskId).then((value) {
await taskDao.rawUpdateData(data, taskId);
taskDao.findTaskById(taskId).then((value) {
print(
'updateTask ${value?.id}, ${utf8.decode(value?.data ?? Uint8List(0))}');
});
Expand Down