From 62961b7ea9fedf8f31624f27444210d5e2c1797f Mon Sep 17 00:00:00 2001 From: yangjiakang Date: Thu, 23 Oct 2025 17:00:02 +0800 Subject: [PATCH] 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 --- examples/catalog_gallery/lib/main.dart | 54 ++++++++++++++++---------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/examples/catalog_gallery/lib/main.dart b/examples/catalog_gallery/lib/main.dart index a3f2b79ca..45ce9d10e 100644 --- a/examples/catalog_gallery/lib/main.dart +++ b/examples/catalog_gallery/lib/main.dart @@ -19,6 +19,21 @@ class CatalogGalleryApp extends StatefulWidget { } class _CatalogGalleryAppState extends State { + + @override + Widget build(BuildContext context) { + return MaterialApp( + theme: ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue), + ), + home: _CatalogGalleryHome(), + ); + } +} + +class _CatalogGalleryHome extends StatelessWidget { + _CatalogGalleryHome(); + final catalog = CoreCatalogItems.asCatalog().copyWithout([ // Excluded, because they are flexible: CoreCatalogItems.tabs, @@ -31,28 +46,27 @@ class _CatalogGalleryAppState extends State { @override Widget build(BuildContext context) { - return MaterialApp( - theme: ThemeData( - colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue), + return Scaffold( + appBar: AppBar( + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + title: const Text('Catalog items that has "exampleData" field set'), ), - home: Scaffold( - appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: const Text('Catalog items that has "exampleData" field set'), - ), - body: DebugCatalogView( - catalog: catalog, - onSubmit: (message) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text( - 'User action: ' - '${jsonEncode(message.parts.last)}', - ), + body: DebugCatalogView( + catalog: catalog, + onSubmit: (message) { + final messageText = message.parts + .whereType() + .map((p) => p.text) + .lastOrNull; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'User action: ' + '${jsonEncode(messageText)}', ), - ); - }, - ), + ), + ); + }, ), ); }