diff --git a/packages/stream_chat_flutter/lib/src/user_list_view.dart b/packages/stream_chat_flutter/lib/src/user_list_view.dart index aa1886b7c..85666413d 100644 --- a/packages/stream_chat_flutter/lib/src/user_list_view.dart +++ b/packages/stream_chat_flutter/lib/src/user_list_view.dart @@ -128,7 +128,7 @@ class UserListView extends StatefulWidget { final int crossAxisCount; /// The builder that will be used in case of error - final Widget Function(Error error)? errorBuilder; + final ErrorBuilder? errorBuilder; /// The builder that will be used to build the list final Widget Function(BuildContext context, List users)? @@ -153,8 +153,8 @@ class _UserListViewState extends State @override Widget build(BuildContext context) { final child = UserListCore( - errorBuilder: widget.errorBuilder as Widget Function(Object)? ?? - (err) => _buildError(err as Error), + errorBuilder: widget.errorBuilder ?? + (BuildContext context, Object err) => _buildError(err), emptyBuilder: widget.emptyBuilder ?? (context) => _buildEmpty(), loadingBuilder: widget.loadingBuilder ?? (context) => LayoutBuilder( @@ -194,52 +194,33 @@ class _UserListViewState extends State bool get isListAlreadySorted => widget.sort?.any((e) => e.field == 'name' && e.direction == 1) ?? false; - Widget _buildError(Error error) { - print(error.stackTrace); - - var message = error.toString(); - if (error is DioError) { - final dioError = error as DioError; - if (dioError.type == DioErrorType.response) { - message = dioError.message; - } else { - message = 'Check your connection and retry'; - } - } - return Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text.rich( - const TextSpan( - children: [ - WidgetSpan( - child: Padding( - padding: EdgeInsets.only( - right: 2, + Widget _buildError(Object error) => Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text.rich( + const TextSpan( + children: [ + WidgetSpan( + child: Padding( + padding: EdgeInsets.only( + right: 2, + ), + child: Icon(Icons.error_outline), ), - child: Icon(Icons.error_outline), ), - ), - TextSpan(text: 'Error loading channels'), - ], + TextSpan(text: 'Error loading users'), + ], + ), + style: Theme.of(context).textTheme.headline6, ), - style: Theme.of(context).textTheme.headline6, - ), - Padding( - padding: const EdgeInsets.only( - top: 16, + TextButton( + onPressed: () => _userListController.loadData!(), + child: const Text('Retry'), ), - child: Text(message), - ), - TextButton( - onPressed: () => _userListController.loadData!(), - child: const Text('Retry'), - ), - ], - ), - ); - } + ], + ), + ); Widget _buildEmpty() => LayoutBuilder( builder: (context, viewportConstraints) => SingleChildScrollView( diff --git a/packages/stream_chat_flutter_core/lib/src/user_list_core.dart b/packages/stream_chat_flutter_core/lib/src/user_list_core.dart index 6377686b5..e7deb356c 100644 --- a/packages/stream_chat_flutter_core/lib/src/user_list_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/user_list_core.dart @@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:stream_chat/stream_chat.dart'; import 'package:stream_chat_flutter_core/src/users_bloc.dart'; +import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart'; /// /// [UserListCore] is a simplified class that allows fetching users while @@ -76,7 +77,7 @@ class UserListCore extends StatefulWidget { final UserListController? userListController; /// The builder that will be used in case of error - final Widget Function(Object error) errorBuilder; + final ErrorBuilder errorBuilder; /// The builder that will be used to build the list final Widget Function(BuildContext context, List users) listBuilder; @@ -170,7 +171,7 @@ class UserListCoreState extends State stream: _buildUserStream(), builder: (context, snapshot) { if (snapshot.hasError) { - return widget.errorBuilder(snapshot.error!); + return widget.errorBuilder(context, snapshot.error!); } if (!snapshot.hasData) { return widget.loadingBuilder(context); diff --git a/packages/stream_chat_flutter_core/test/user_list_core_test.dart b/packages/stream_chat_flutter_core/test/user_list_core_test.dart index 6f21123f6..95077e5cb 100644 --- a/packages/stream_chat_flutter_core/test/user_list_core_test.dart +++ b/packages/stream_chat_flutter_core/test/user_list_core_test.dart @@ -43,7 +43,7 @@ void main() { listBuilder: (_, __) => Offstage(), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), ); await tester.pumpWidget(userListCore); @@ -62,7 +62,7 @@ void main() { listBuilder: (_, __) => Offstage(), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), ); final mockClient = MockClient(); @@ -91,7 +91,7 @@ void main() { listBuilder: (_, __) => Offstage(), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), userListController: controller, ); @@ -125,7 +125,8 @@ void main() { listBuilder: (_, __) => Offstage(), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Container(key: errorWidgetKey), + errorBuilder: (BuildContext context, Object error) => + Container(key: errorWidgetKey), ); final mockClient = MockClient(); @@ -170,7 +171,7 @@ void main() { listBuilder: (_, __) => Offstage(), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Container(key: emptyWidgetKey), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), ); final mockClient = MockClient(); @@ -215,7 +216,7 @@ void main() { listBuilder: (_, __) => Container(key: listWidgetKey), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), ); final mockClient = MockClient(); @@ -273,7 +274,7 @@ void main() { ), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), groupAlphabetically: true, ); @@ -341,7 +342,7 @@ void main() { ), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), pagination: pagination, groupAlphabetically: true, ); @@ -446,7 +447,7 @@ void main() { ), loadingBuilder: (BuildContext context) => Offstage(), emptyBuilder: (BuildContext context) => Offstage(), - errorBuilder: (Object error) => Offstage(), + errorBuilder: (BuildContext context, Object error) => Offstage(), pagination: pagination.copyWith(limit: limit), groupAlphabetically: true, );