From a3a88aead10d21dc33936a6bce8cb8e7d171f1af Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 28 Jun 2021 13:08:51 +0200 Subject: [PATCH 1/3] fix user list error handling --- .../stream_chat_flutter/lib/src/user_list_view.dart | 12 +++++------- .../lib/src/user_list_core.dart | 5 +++-- 2 files changed, 8 insertions(+), 9 deletions(-) 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..f915fcb04 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,12 +194,10 @@ 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); - + Widget _buildError(Object error) { var message = error.toString(); if (error is DioError) { - final dioError = error as DioError; + final dioError = error; if (dioError.type == DioErrorType.response) { message = dioError.message; } else { 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); From 8052edf09c37445134c090c5e06ed950f346f045 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 28 Jun 2021 15:20:50 +0200 Subject: [PATCH 2/3] fix tests --- .../test/user_list_core_test.dart | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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, ); From 29f234f69bdc2ef0890d313e47691b53e0e54844 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 29 Jun 2021 11:09:27 +0200 Subject: [PATCH 3/3] update error builder --- .../lib/src/user_list_view.dart | 63 +++++++------------ 1 file changed, 23 insertions(+), 40 deletions(-) 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 f915fcb04..85666413d 100644 --- a/packages/stream_chat_flutter/lib/src/user_list_view.dart +++ b/packages/stream_chat_flutter/lib/src/user_list_view.dart @@ -194,50 +194,33 @@ class _UserListViewState extends State bool get isListAlreadySorted => widget.sort?.any((e) => e.field == 'name' && e.direction == 1) ?? false; - Widget _buildError(Object error) { - var message = error.toString(); - if (error is DioError) { - final dioError = error; - 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(