From fe69bced878d8d0ccb1f9b60663f48f998589b7b Mon Sep 17 00:00:00 2001 From: micimize Date: Sun, 16 May 2021 15:21:55 -0700 Subject: [PATCH] fix(examples): upgrade to nullsafety --- .circleci/config.yml | 2 +- .../graphql/test/graphql_client_test.dart | 2 +- .../example/lib/fetchmore/main.dart | 24 +++++---- .../example/lib/graphql_bloc/bloc.dart | 36 ++++++------- .../example/lib/graphql_bloc/main.dart | 30 +++++------ .../example/lib/graphql_widget/main.dart | 50 +++++++++---------- packages/graphql_flutter/example/pubspec.yaml | 2 +- 7 files changed, 74 insertions(+), 72 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 649dca8d0..198ceb773 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,7 +1,7 @@ version: 2.1 aliases: - &flutter_environment - - image: cirrusci/flutter:2.3.0-1.0.pre + - image: cirrusci/flutter:stable - &node_environment - image: circleci/node:10 - &restore_cache diff --git a/packages/graphql/test/graphql_client_test.dart b/packages/graphql/test/graphql_client_test.dart index 675727ad2..1cd623503 100644 --- a/packages/graphql/test/graphql_client_test.dart +++ b/packages/graphql/test/graphql_client_test.dart @@ -454,7 +454,7 @@ void main() { ), ); - final observableQuery = await client.watchQuery(WatchQueryOptions( + final observableQuery = client.watchQuery(WatchQueryOptions( document: _options.document, variables: _options.variables, fetchResults: false, diff --git a/packages/graphql_flutter/example/lib/fetchmore/main.dart b/packages/graphql_flutter/example/lib/fetchmore/main.dart index 120fbe81e..dfa5f98a7 100644 --- a/packages/graphql_flutter/example/lib/fetchmore/main.dart +++ b/packages/graphql_flutter/example/lib/fetchmore/main.dart @@ -38,11 +38,11 @@ class FetchMoreWidgetScreen extends StatelessWidget { class MyHomePage extends StatefulWidget { const MyHomePage({ - Key key, + Key? key, this.title, }) : super(key: key); - final String title; + final String? title; @override _MyHomePageState createState() => _MyHomePageState(); @@ -55,7 +55,7 @@ class _MyHomePageState extends State { void changeQuery(String query) { setState(() { print(query); - _searchQuery = query ?? 'flutter'; + _searchQuery = query; }); } @@ -63,7 +63,7 @@ class _MyHomePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text(widget.title), + title: Text(widget.title!), ), body: Container( padding: const EdgeInsets.symmetric(horizontal: 8.0), @@ -89,7 +89,7 @@ class _MyHomePageState extends State { }, //pollInterval: 10, ), - builder: (QueryResult result, {refetch, FetchMore fetchMore}) { + builder: (QueryResult result, {refetch, FetchMore? fetchMore}) { if (result.hasException) { return Text(result.exception.toString()); } @@ -107,10 +107,10 @@ class _MyHomePageState extends State { // result.data can be either a [List] or a [Map] final repositories = - (result.data['search']['nodes'] as List); + (result.data!['search']['nodes'] as List); - final Map pageInfo = result.data['search']['pageInfo']; - final String fetchMoreCursor = pageInfo['endCursor']; + final Map pageInfo = result.data!['search']['pageInfo']; + final String? fetchMoreCursor = pageInfo['endCursor']; final opts = FetchMoreOptions( variables: {'cursor': fetchMoreCursor}, updateQuery: (previousResultData, fetchMoreResultData) { @@ -118,8 +118,10 @@ class _MyHomePageState extends State { // in this case, we want to display previous repos plus next repos // so, we combine data in both into a single list of repos final repos = [ - ...previousResultData['search']['nodes'] as List, - ...fetchMoreResultData['search']['nodes'] as List + ...previousResultData!['search']['nodes'] + as List, + ...fetchMoreResultData!['search']['nodes'] + as List ]; // to avoid alot of work, lets just update the list of repos in returned @@ -155,7 +157,7 @@ class _MyHomePageState extends State { style: Theme.of(context).textTheme.caption), ElevatedButton( onPressed: () { - fetchMore(opts); + fetchMore!(opts); }, child: Row( mainAxisAlignment: MainAxisAlignment.center, diff --git a/packages/graphql_flutter/example/lib/graphql_bloc/bloc.dart b/packages/graphql_flutter/example/lib/graphql_bloc/bloc.dart index 88c457d8b..6b0d89630 100644 --- a/packages/graphql_flutter/example/lib/graphql_bloc/bloc.dart +++ b/packages/graphql_flutter/example/lib/graphql_bloc/bloc.dart @@ -9,9 +9,9 @@ import '../local.dart'; class Repo { const Repo({this.id, this.name, this.viewerHasStarred}); - final String id; - final String name; - final bool viewerHasStarred; + final String? id; + final String? name; + final bool? viewerHasStarred; } class Bloc { @@ -23,29 +23,29 @@ class Bloc { // @todo handle error final _ = await _mutateToggleStar(t); - _repoSubject.add(_repoSubject.value.map((Repo e) { + _repoSubject.add(_repoSubject.value!.map((Repo e) { if (e.id != t.id) { return e; } return Repo( - id: t.id, name: t.name, viewerHasStarred: !t.viewerHasStarred); + id: t.id, name: t.name, viewerHasStarred: !t.viewerHasStarred!); }).toList()); _toggleStarLoadingSubject.add(null); }); } - final BehaviorSubject> _repoSubject = - BehaviorSubject>(); - Stream> get repoStream => _repoSubject.stream; + final BehaviorSubject?> _repoSubject = + BehaviorSubject?>(); + Stream?> get repoStream => _repoSubject.stream; final ReplaySubject _toggleStarSubject = ReplaySubject(); Sink get toggleStarSink => _toggleStarSubject; /// The repo currently loading, if any - final BehaviorSubject _toggleStarLoadingSubject = - BehaviorSubject(); + final BehaviorSubject _toggleStarLoadingSubject = + BehaviorSubject(); - Stream get toggleStarLoadingStream => + Stream get toggleStarLoadingStream => _toggleStarLoadingSubject.stream; final BehaviorSubject _updateNumberOfRepo = BehaviorSubject(); @@ -71,8 +71,8 @@ class Bloc { Future _mutateToggleStar(Repo repo) async { final _options = MutationOptions( document: - gql(repo.viewerHasStarred ? mutations.removeStar : mutations.addStar), - variables: { + gql(repo.viewerHasStarred! ? mutations.removeStar : mutations.addStar), + variables: { 'starrableId': repo.id, }, ); @@ -96,19 +96,19 @@ class Bloc { final result = await _client.query(_options); if (result.hasException) { - _repoSubject.addError(result.exception); + _repoSubject.addError(result.exception!); return; } // result.data can be either a [List] or a [Map] final repositories = - result.data['viewer']['repositories']['nodes'] as List; + result.data!['viewer']['repositories']['nodes'] as List; _repoSubject.add(repositories .map((dynamic e) => Repo( - id: e['id'] as String, - name: e['name'] as String, - viewerHasStarred: e['viewerHasStarred'] as bool, + id: e['id'] as String?, + name: e['name'] as String?, + viewerHasStarred: e['viewerHasStarred'] as bool?, )) .toList()); } diff --git a/packages/graphql_flutter/example/lib/graphql_bloc/main.dart b/packages/graphql_flutter/example/lib/graphql_bloc/main.dart index 5c093a6a1..4613d44d9 100644 --- a/packages/graphql_flutter/example/lib/graphql_bloc/main.dart +++ b/packages/graphql_flutter/example/lib/graphql_bloc/main.dart @@ -4,7 +4,7 @@ import 'bloc.dart' show Bloc, Repo; class GraphQLBlocPatternScreen extends StatefulWidget { GraphQLBlocPatternScreen({ - Key key, + Key? key, this.title = 'GraphQL Widget', }) : bloc = Bloc(), super(key: key); @@ -40,10 +40,10 @@ class _MyHomePageState extends State { onChanged: (String n) => bloc.updateNumberOfRepoSink.add(int.parse(n)), ), - StreamBuilder>( + StreamBuilder?>( stream: bloc.repoStream, builder: - (BuildContext context, AsyncSnapshot> snapshot) { + (BuildContext context, AsyncSnapshot?> snapshot) { if (snapshot.hasError) { return Text('\nErrors: \n ' + (snapshot.error as List).join(',\n ')); @@ -54,7 +54,7 @@ class _MyHomePageState extends State { ); } - final repositories = snapshot.data; + final repositories = snapshot.data!; return Expanded( child: ListView.builder( @@ -75,42 +75,42 @@ class _MyHomePageState extends State { class StarrableRepository extends StatelessWidget { const StarrableRepository({ - Key key, - @required this.repository, - @required this.bloc, + Key? key, + required this.repository, + required this.bloc, }) : super(key: key); final Bloc bloc; final Repo repository; - Map extractRepositoryData(Map data) { - final action = data['action'] as Map; + Map? extractRepositoryData(Map data) { + final action = data['action'] as Map?; if (action == null) { return null; } - return action['starrable'] as Map; + return action['starrable'] as Map?; } - bool get viewerHasStarred => repository.viewerHasStarred; + bool? get viewerHasStarred => repository.viewerHasStarred; @override Widget build(BuildContext context) { - return StreamBuilder( + return StreamBuilder( stream: bloc.toggleStarLoadingStream, initialData: null, - builder: (BuildContext context, AsyncSnapshot result) { + builder: (BuildContext context, AsyncSnapshot result) { final loading = repository.id == result.data; return ListTile( - leading: viewerHasStarred + leading: viewerHasStarred! ? const Icon( Icons.star, color: Colors.amber, ) : const Icon(Icons.star_border), trailing: loading ? const CircularProgressIndicator() : null, - title: Text(repository.name), + title: Text(repository.name!), onTap: () { bloc.toggleStarSink.add(repository); }, diff --git a/packages/graphql_flutter/example/lib/graphql_widget/main.dart b/packages/graphql_flutter/example/lib/graphql_widget/main.dart index a6da69109..d02ca0494 100644 --- a/packages/graphql_flutter/example/lib/graphql_widget/main.dart +++ b/packages/graphql_flutter/example/lib/graphql_widget/main.dart @@ -54,11 +54,11 @@ class GraphQLWidgetScreen extends StatelessWidget { class MyHomePage extends StatefulWidget { const MyHomePage({ - Key key, + Key? key, this.title, }) : super(key: key); - final String title; + final String? title; @override _MyHomePageState createState() => _MyHomePageState(); @@ -69,7 +69,7 @@ class _MyHomePageState extends State { void changeQuery(String number) { setState(() { - nRepositories = int.parse(number) ?? 50; + nRepositories = int.parse(number); }); } @@ -77,7 +77,7 @@ class _MyHomePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text(widget.title), + title: Text(widget.title!), ), body: Container( padding: const EdgeInsets.symmetric(horizontal: 8.0), @@ -110,7 +110,7 @@ class _MyHomePageState extends State { } // result.data can be either a [List] or a [Map] - final repositories = (result.data['viewer']['repositories'] + final repositories = (result.data!['viewer']['repositories'] ['nodes'] as List); return Expanded( @@ -147,25 +147,25 @@ class _MyHomePageState extends State { class StarrableRepository extends StatelessWidget { const StarrableRepository({ - Key key, - @required this.repository, - @required this.optimistic, + Key? key, + required this.repository, + required this.optimistic, }) : super(key: key); final Map repository; final bool optimistic; /// Extract the repository data for updating the fragment - Map extractRepositoryData(Map data) { - final action = data['action'] as Map; + Map? extractRepositoryData(Map data) { + final action = data['action'] as Map?; if (action == null) { return null; } - return action['starrable'] as Map; + return action['starrable'] as Map?; } /// Get whether the repository is currently starred, according to the current Query - bool get starred => repository['viewerHasStarred'] as bool; + bool? get starred => repository['viewerHasStarred'] as bool?; /// Build an optimisticResult based on whether [viewerIsStarrring] Map expectedResult(bool viewerIsStarrring) => @@ -180,12 +180,12 @@ class StarrableRepository extends StatelessWidget { }; OnMutationUpdate get update => (cache, result) { - if (result.hasException) { + if (result!.hasException) { print(result.exception); } else { final updated = { ...repository, - ...extractRepositoryData(result.data), + ...extractRepositoryData(result.data!)!, }; cache.writeFragment( Fragment( @@ -216,34 +216,34 @@ class StarrableRepository extends StatelessWidget { options: MutationOptions( document: gql(mutations.addStar), update: update, - onError: (OperationException error) => + onError: (OperationException? error) => _simpleAlert(context, error.toString()), onCompleted: (dynamic resultData) => _simpleAlert(context, 'Thanks for your star!'), // 'Sorry you changed your mind!', ), - builder: (RunMutation _addStar, QueryResult addResult) { + builder: (RunMutation _addStar, QueryResult? addResult) { final addStar = () => _addStar({'starrableId': repository['id']}, optimisticResult: expectedResult(true)); return Mutation( options: MutationOptions( document: gql(mutations.removeStar), update: update, - onError: (OperationException error) => + onError: (OperationException? error) => _simpleAlert(context, error.toString()), onCompleted: (dynamic resultData) => _simpleAlert(context, 'Sorry you changed your mind!'), ), - builder: (RunMutation _removeStar, QueryResult removeResult) { + builder: (RunMutation _removeStar, QueryResult? removeResult) { final removeStar = () => _removeStar( {'starrableId': repository['id']}, optimisticResult: expectedResult(false)); final anyLoading = - addResult.isLoading || removeResult.isLoading || optimistic; + addResult!.isLoading || removeResult!.isLoading || optimistic; return ListTile( - leading: starred + leading: starred! ? Icon( Icons.star, color: Colors.amber, @@ -253,10 +253,10 @@ class StarrableRepository extends StatelessWidget { title: Text(repository['name'] as String), /// uncomment this line to see the actual mutation results - subtitle: _debugLatestResults(addResult, removeResult), + subtitle: _debugLatestResults(addResult, removeResult!), onTap: anyLoading ? null - : starred + : starred! ? removeStar : addStar, ); @@ -280,15 +280,15 @@ class StarrableRepository extends StatelessWidget { /// This will cause the mutation results to be rebroadcast from the cache, /// merging in the new `Repository.viewerHasStarred` state. /// This can be desirable when a mutation result is used merely as a follow-up query. - Widget _debugLatestResults(QueryResult add, QueryResult remove) { + Widget? _debugLatestResults(QueryResult add, QueryResult remove) { //return null; var latestResults = ''; if (add.data != null) { - latestResults += 'addResultRepo: ${extractRepositoryData(add.data)}; '; + latestResults += 'addResultRepo: ${extractRepositoryData(add.data!)}; '; } if (remove.data != null) { latestResults += - 'removeResultRepo: ${extractRepositoryData(remove.data)}; '; + 'removeResultRepo: ${extractRepositoryData(remove.data!)}; '; } if (latestResults.isEmpty) { return null; diff --git a/packages/graphql_flutter/example/pubspec.yaml b/packages/graphql_flutter/example/pubspec.yaml index c24c93713..77c039666 100644 --- a/packages/graphql_flutter/example/pubspec.yaml +++ b/packages/graphql_flutter/example/pubspec.yaml @@ -27,4 +27,4 @@ dependency_overrides: path: ../../graphql environment: - sdk: '>=2.10.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0'