Skip to content

Commit 62961b7

Browse files
committed
Fix runtime errors in catalog_gallery example
- Fix ScaffoldMessenger error by moving UI code to _CatalogGalleryHome - Fix JSON encoding error by using messageText instead of jsonEncode(message.parts.last) - Resolves two runtime exceptions that prevented the example from running properly
1 parent 6de15ad commit 62961b7

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

examples/catalog_gallery/lib/main.dart

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ class CatalogGalleryApp extends StatefulWidget {
1919
}
2020

2121
class _CatalogGalleryAppState extends State<CatalogGalleryApp> {
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return MaterialApp(
26+
theme: ThemeData(
27+
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
28+
),
29+
home: _CatalogGalleryHome(),
30+
);
31+
}
32+
}
33+
34+
class _CatalogGalleryHome extends StatelessWidget {
35+
_CatalogGalleryHome();
36+
2237
final catalog = CoreCatalogItems.asCatalog().copyWithout([
2338
// Excluded, because they are flexible:
2439
CoreCatalogItems.tabs,
@@ -31,28 +46,27 @@ class _CatalogGalleryAppState extends State<CatalogGalleryApp> {
3146

3247
@override
3348
Widget build(BuildContext context) {
34-
return MaterialApp(
35-
theme: ThemeData(
36-
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
49+
return Scaffold(
50+
appBar: AppBar(
51+
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
52+
title: const Text('Catalog items that has "exampleData" field set'),
3753
),
38-
home: Scaffold(
39-
appBar: AppBar(
40-
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
41-
title: const Text('Catalog items that has "exampleData" field set'),
42-
),
43-
body: DebugCatalogView(
44-
catalog: catalog,
45-
onSubmit: (message) {
46-
ScaffoldMessenger.of(context).showSnackBar(
47-
SnackBar(
48-
content: Text(
49-
'User action: '
50-
'${jsonEncode(message.parts.last)}',
51-
),
54+
body: DebugCatalogView(
55+
catalog: catalog,
56+
onSubmit: (message) {
57+
final messageText = message.parts
58+
.whereType<TextPart>()
59+
.map((p) => p.text)
60+
.lastOrNull;
61+
ScaffoldMessenger.of(context).showSnackBar(
62+
SnackBar(
63+
content: Text(
64+
'User action: '
65+
'${jsonEncode(messageText)}',
5266
),
53-
);
54-
},
55-
),
67+
),
68+
);
69+
},
5670
),
5771
);
5872
}

0 commit comments

Comments
 (0)