Skip to content

Commit

Permalink
fix(flutter): make sure starwars works with nullsafe changes
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed Mar 7, 2021
1 parent e909a77 commit 6d27c64
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 50 deletions.
19 changes: 0 additions & 19 deletions examples/starwars/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
97C146EC1CF9000F007C117D /* Resources */,
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
09EF7CC3F13D32A43F4A3854 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -210,24 +209,6 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
09EF7CC3F13D32A43F4A3854 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../Flutter/Flutter.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/graphql/lib/src/core/fetch_more.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Future<QueryResult> fetchMoreImplementation(
required QueryOptions originalOptions,
required QueryManager queryManager,
required QueryResult previousResult,
required String queryId,
String? queryId,
}) async {
// fetch more and udpate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ class GraphQLConsumer extends StatelessWidget {
required this.builder,
}) : super(key: key);

final GraphQLConsumerBuilder? builder;
final GraphQLConsumerBuilder builder;

@override
Widget build(BuildContext context) {
/// Gets the client from the closest wrapping [GraphQLProvider].
final GraphQLClient client = GraphQLProvider.of(context).value;
return builder!(client);
return builder(GraphQLProvider.of(context).value);
}
}
16 changes: 8 additions & 8 deletions packages/graphql_flutter/lib/src/widgets/graphql_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ class GraphQLProvider extends StatefulWidget {
final ValueNotifier<GraphQLClient>? client;
final Widget? child;

static ValueNotifier<GraphQLClient> of(BuildContext context) {
final _InheritedGraphQLProvider inheritedGraphqlProvider =
_InheritedGraphQLProvider.of(context);

return inheritedGraphqlProvider.client;
}
static ValueNotifier<GraphQLClient> of(BuildContext context) =>
_InheritedGraphQLProvider.of(context).client;

@override
State<StatefulWidget> createState() => _GraphQLProviderState();
Expand Down Expand Up @@ -56,8 +52,12 @@ class _InheritedGraphQLProvider extends InheritedWidget {
}) : clientValue = client.value,
super(child: child);

factory _InheritedGraphQLProvider.of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<_InheritedGraphQLProvider>()!;
factory _InheritedGraphQLProvider.of(BuildContext context) {
final provider =
context.dependOnInheritedWidgetOfExactType<_InheritedGraphQLProvider>();
assert(provider != null);
return provider!;
}

final ValueNotifier<GraphQLClient> client;
final GraphQLClient clientValue;
Expand Down
29 changes: 15 additions & 14 deletions packages/graphql_flutter/lib/src/widgets/result_accumulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import 'package:flutter/widgets.dart';
/// Same as a fold combine for lists
///
/// Return `null` to avoid setting state.
typedef Accumulator<Element> = List<Element> Function(
List<Element>? previousValue,
typedef Accumulator<Element> = List<Element>? Function(
List<Element> previousValue,
Element element,
);

List<T> _append<T>(List<T> results, T latest) => [...results, latest];
List<T>? _append<T>(List<T> results, T latest) => [...results, latest];

List<T>? _appendUnique<T>(List<T> results, T latest) {
if (!results.contains(latest)) {
Expand All @@ -24,13 +24,13 @@ class ResultAccumulator<T> extends StatefulWidget {
const ResultAccumulator({
required this.latest,
required this.builder,
Accumulator<T?>? accumulator,
}) : accumulator = accumulator ?? _append as List<T?> Function(List<T?>?, T?);
Accumulator<T>? accumulator,
}) : accumulator = accumulator ?? _append;

const ResultAccumulator.appendUniqueEntries({
required this.latest,
required this.builder,
}) : accumulator = _appendUnique as List<T?> Function(List<T?>?, T?);
}) : accumulator = _appendUnique;

/// The latest entry in the stream
final T latest;
Expand All @@ -39,7 +39,7 @@ class ResultAccumulator<T> extends StatefulWidget {
/// to prevent a call to `setState`.
///
/// Defaults to `(results, latest) => [...results, latest]`
final Accumulator<T?> accumulator;
final Accumulator<T> accumulator;

/// Builds the resulting widget with all accumulated results.
final Widget Function(BuildContext, {required List<T>? results}) builder;
Expand All @@ -48,8 +48,8 @@ class ResultAccumulator<T> extends StatefulWidget {
_ResultAccumulatorState createState() => _ResultAccumulatorState<T>();
}

class _ResultAccumulatorState<T> extends State<ResultAccumulator<T?>> {
List<T?>? results;
class _ResultAccumulatorState<T> extends State<ResultAccumulator<T>> {
List<T> results = [];

@override
void initState() {
Expand All @@ -58,14 +58,15 @@ class _ResultAccumulatorState<T> extends State<ResultAccumulator<T?>> {
}

@override
void didUpdateWidget(ResultAccumulator<T?> oldWidget) {
void didUpdateWidget(ResultAccumulator<T> oldWidget) {
super.didUpdateWidget(oldWidget);

final newResults = widget.accumulator(results, widget.latest);

setState(() {
results = newResults;
});
if (newResults != null) {
setState(() {
results = newResults;
});
}
}

@override
Expand Down
1 change: 1 addition & 0 deletions packages/graphql_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
path: ^1.8.0
connectivity: ^3.0.2
hive: ^2.0.0
plugin_platform_interface: ^2.0.0
dev_dependencies:
pedantic: ^1.11.0
mockito: ^5.0.0
Expand Down
5 changes: 4 additions & 1 deletion packages/graphql_flutter/test/graphql_consumer_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';

import 'package:graphql_flutter/src/widgets/graphql_consumer.dart'
show GraphQLConsumer;
Expand All @@ -7,8 +8,10 @@ void main() {
group('GraphQLConsumer', () {
testWidgets('raises assertion error because of lack of GraphQLProvider',
(WidgetTester tester) async {
await tester.pumpWidget(const GraphQLConsumer(builder: null));
await tester.pumpWidget(const GraphQLConsumer(builder: _b));
expect(tester.takeException(), isAssertionError);
});
});
}

Widget _b(_) => Container();
6 changes: 3 additions & 3 deletions packages/graphql_flutter/test/widgets/query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class Page extends StatefulWidget {
}

class PageState extends State<Page> {
Map<String, dynamic>? variables;
Map<String, dynamic> variables = const {};
FetchPolicy? fetchPolicy;
ErrorPolicy? errorPolicy;

@override
void initState() {
super.initState();
variables = widget.variables;
variables = widget.variables ?? const {};
fetchPolicy = widget.fetchPolicy;
errorPolicy = widget.errorPolicy;
}
Expand Down Expand Up @@ -98,7 +98,7 @@ class PageState extends State<Page> {
return Query(
options: QueryOptions(
document: query,
variables: variables!,
variables: variables,
fetchPolicy: fetchPolicy,
errorPolicy: errorPolicy,
),
Expand Down

0 comments on commit 6d27c64

Please sign in to comment.