diff --git a/packages/fl_query/example/lib/pages/query.dart b/packages/fl_query/example/lib/pages/query.dart index f4ff629..e7e4f88 100644 --- a/packages/fl_query/example/lib/pages/query.dart +++ b/packages/fl_query/example/lib/pages/query.dart @@ -29,13 +29,15 @@ class QueryPage extends StatelessWidget { 'hello', () { return Future.delayed( - const Duration(seconds: 6), () => 'Hello World! $value'); + const Duration(seconds: 6), + () => 'Hello World! $value', + ); }, initial: 'Hello', - jsonConfig: JsonConfig( - fromJson: (json) => json['data'], - toJson: (data) => {'data': data}, - ), + // jsonConfig: JsonConfig( + // fromJson: (json) => json['data'], + // toJson: (data) => {'data': data}, + // ), onData: (value) { debugPrint('onData: $value'); }, diff --git a/packages/fl_query/lib/src/core/query.dart b/packages/fl_query/lib/src/core/query.dart index 4cea4d8..91a2762 100644 --- a/packages/fl_query/lib/src/core/query.dart +++ b/packages/fl_query/lib/src/core/query.dart @@ -21,23 +21,28 @@ class QueryState with Invalidation { final DateTime updatedAt; final Duration staleDuration; + final bool _loading; + const QueryState({ this.data, this.error, required this.updatedAt, required this.staleDuration, - }); + bool loading = false, + }) : _loading = loading; QueryState copyWith({ DataType? data, ErrorType? error, DateTime? updatedAt, + bool? loading, }) { return QueryState( updatedAt: updatedAt ?? this.updatedAt, staleDuration: staleDuration, data: data ?? this.data, error: error ?? this.error, + loading: loading ?? this._loading, ); } } @@ -125,9 +130,11 @@ class Query StreamSubscription? _connectivitySubscription; bool get isInitial => hasData && state.data == _initial; - bool get isLoading => isInitial ? _mutex.isLocked : !hasData && !hasError; + bool get isLoading => + (isInitial && !hasError && state._loading) || + (!hasData && !hasError && state._loading); bool get isRefreshing => - ((!isInitial && hasData) || hasError) && _mutex.isLocked; + ((!isInitial && hasData) || hasError) && state._loading; bool get isInactive => !hasListeners; bool get hasData => state.data != null; bool get hasError => state.error != null; @@ -145,7 +152,7 @@ class Query return; } return _mutex.protect(() async { - state = state.copyWith(); + state = state.copyWith(loading: true); _operation = cancellableRetryOperation( _queryFn, config: retryConfig, @@ -154,6 +161,7 @@ class Query data: data, error: null, updatedAt: DateTime.now(), + loading: false, ); if (data is DataType) { _dataController.add(data); @@ -166,7 +174,11 @@ class Query } }, onFailed: (ErrorType? error) { - state = state.copyWith(error: error, updatedAt: DateTime.now()); + state = state.copyWith( + error: error, + updatedAt: DateTime.now(), + loading: false, + ); if (error is ErrorType) _errorController.add(error); }, ); @@ -193,12 +205,20 @@ class Query } void setData(DataType data) { - state = state.copyWith(data: data, updatedAt: DateTime.now()); + state = state.copyWith( + data: data, + updatedAt: DateTime.now(), + loading: false, + ); } Future reset() async { await _operation?.cancel(); - state = state.copyWith(data: _initial, updatedAt: DateTime.now()); + state = state.copyWith( + data: _initial, + updatedAt: DateTime.now(), + loading: false, + ); _box.delete(key); }