Skip to content

Commit e021a7e

Browse files
committed
Only use sync streams after opt-in
1 parent 1614d4a commit e021a7e

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

demos/supabase-todolist/lib/app_config_template.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ class AppConfig {
66
static const String powersyncUrl = 'https://foo.powersync.journeyapps.com';
77
static const String supabaseStorageBucket =
88
''; // Optional. Only required when syncing attachments and using Supabase Storage. See packages/powersync_attachments_helper.
9+
// Whether the PowerSync instance uses sync streams to make fetching todo
10+
// items optional.
11+
static const bool hasSyncStreams = true;
912
}

demos/supabase-todolist/lib/widgets/todo_list_page.dart

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:powersync/powersync.dart';
33

4+
import '../app_config.dart';
45
import '../powersync.dart';
56
import './status_app_bar.dart';
67
import './todo_item_dialog.dart';
@@ -33,22 +34,54 @@ class TodoListPage extends StatelessWidget {
3334
);
3435

3536
return Scaffold(
36-
appBar: StatusAppBar(title: Text(list.name)),
37-
floatingActionButton: button,
38-
body: TodoListWidget(list: list));
37+
appBar: StatusAppBar(title: Text(list.name)),
38+
floatingActionButton: button,
39+
body: AppConfig.hasSyncStreams
40+
? _SyncStreamTodoListWidget(list: list)
41+
: TodoListWidget(list: list),
42+
);
3943
}
4044
}
4145

42-
class TodoListWidget extends StatefulWidget {
46+
class TodoListWidget extends StatelessWidget {
4347
final TodoList list;
4448

4549
const TodoListWidget({super.key, required this.list});
4650

4751
@override
48-
State<TodoListWidget> createState() => _TodoListWidgetState();
52+
Widget build(BuildContext context) {
53+
return StreamBuilder(
54+
stream: TodoList.watchSyncStatus().map((e) => e.hasSynced),
55+
initialData: db.currentStatus.hasSynced,
56+
builder: (context, snapshot) {
57+
return StreamBuilder(
58+
stream: list.watchItems(),
59+
builder: (context, snapshot) {
60+
final items = snapshot.data ?? const [];
61+
62+
return ListView(
63+
padding: const EdgeInsets.symmetric(vertical: 8.0),
64+
children: items.map((todo) {
65+
return TodoItemWidget(todo: todo);
66+
}).toList(),
67+
);
68+
},
69+
);
70+
},
71+
);
72+
}
73+
}
74+
75+
class _SyncStreamTodoListWidget extends StatefulWidget {
76+
final TodoList list;
77+
78+
const _SyncStreamTodoListWidget({required this.list});
79+
80+
@override
81+
State<_SyncStreamTodoListWidget> createState() => _SyncStreamTodosState();
4982
}
5083

51-
class _TodoListWidgetState extends State<TodoListWidget> {
84+
class _SyncStreamTodosState extends State<_SyncStreamTodoListWidget> {
5285
SyncStreamSubscription? _listSubscription;
5386

5487
void _subscribe(String listId) {
@@ -73,7 +106,7 @@ class _TodoListWidgetState extends State<TodoListWidget> {
73106
}
74107

75108
@override
76-
void didUpdateWidget(covariant TodoListWidget oldWidget) {
109+
void didUpdateWidget(covariant _SyncStreamTodoListWidget oldWidget) {
77110
super.didUpdateWidget(oldWidget);
78111
_subscribe(widget.list.id);
79112
}

0 commit comments

Comments
 (0)