Skip to content

Commit 2ceb667

Browse files
Example: handle task stream error case.
1 parent 09d1d94 commit 2ceb667

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

objectbox/example/flutter/objectbox_demo_relations/lib/tasklist_elements.dart

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,35 @@ class _TaskListState extends State<TaskList> {
9999
return Expanded(
100100
child: StreamBuilder<List<Task>>(
101101
stream: objectbox.getTasks(),
102-
builder: (context, snapshot) => ListView.builder(
103-
shrinkWrap: true,
104-
padding: const EdgeInsets.symmetric(horizontal: 20.0),
105-
itemCount: snapshot.hasData ? snapshot.data!.length : 0,
106-
itemBuilder: _itemBuilder(snapshot.data ?? []))));
102+
builder: (context, snapshot) {
103+
if (snapshot.hasError) {
104+
// Print the stack trace and show the error message.
105+
// An actual app would display a user-friendly error message
106+
// and report the error behind the scenes.
107+
debugPrintStack(stackTrace: snapshot.stackTrace);
108+
return Column(
109+
mainAxisAlignment: MainAxisAlignment.center,
110+
children: [
111+
const Icon(
112+
Icons.error_outline,
113+
color: Colors.red,
114+
size: 60,
115+
),
116+
Padding(
117+
padding:
118+
const EdgeInsets.only(top: 16, left: 16, right: 16),
119+
child: Text('Error: ${snapshot.error}'),
120+
),
121+
],
122+
);
123+
} else {
124+
return ListView.builder(
125+
shrinkWrap: true,
126+
padding: const EdgeInsets.symmetric(horizontal: 20.0),
127+
itemCount: snapshot.hasData ? snapshot.data!.length : 0,
128+
itemBuilder: _itemBuilder(snapshot.data ?? []));
129+
}
130+
}));
107131
}
108132
}
109133

0 commit comments

Comments
 (0)