Skip to content

Commit

Permalink
Merge pull request #511 from GetStream/hotfix/userListErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
xsahil03x authored Jun 29, 2021
2 parents 3885f0e + 29f234f commit 5cb87d2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 56 deletions.
71 changes: 26 additions & 45 deletions packages/stream_chat_flutter/lib/src/user_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListItem> users)?
Expand All @@ -153,8 +153,8 @@ class _UserListViewState extends State<UserListView>
@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(
Expand Down Expand Up @@ -194,52 +194,33 @@ class _UserListViewState extends State<UserListView>
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: <Widget>[
Text.rich(
const TextSpan(
children: [
WidgetSpan(
child: Padding(
padding: EdgeInsets.only(
right: 2,
Widget _buildError(Object error) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
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(
Expand Down
5 changes: 3 additions & 2 deletions packages/stream_chat_flutter_core/lib/src/user_list_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ListItem> users) listBuilder;
Expand Down Expand Up @@ -170,7 +171,7 @@ class UserListCoreState extends State<UserListCore>
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);
Expand Down
19 changes: 10 additions & 9 deletions packages/stream_chat_flutter_core/test/user_list_core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
);
Expand Down

0 comments on commit 5cb87d2

Please sign in to comment.