Skip to content

Commit

Permalink
fix(examples): upgrade to nullsafety
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed May 16, 2021
1 parent cbbcf2b commit fe69bce
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/test/graphql_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 13 additions & 11 deletions packages/graphql_flutter/example/lib/fetchmore/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -55,15 +55,15 @@ class _MyHomePageState extends State<MyHomePage> {
void changeQuery(String query) {
setState(() {
print(query);
_searchQuery = query ?? 'flutter';
_searchQuery = query;
});
}

@override
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),
Expand All @@ -89,7 +89,7 @@ class _MyHomePageState extends State<MyHomePage> {
},
//pollInterval: 10,
),
builder: (QueryResult result, {refetch, FetchMore fetchMore}) {
builder: (QueryResult result, {refetch, FetchMore? fetchMore}) {
if (result.hasException) {
return Text(result.exception.toString());
}
Expand All @@ -107,19 +107,21 @@ class _MyHomePageState extends State<MyHomePage> {

// result.data can be either a [List<dynamic>] or a [Map<String, dynamic>]
final repositories =
(result.data['search']['nodes'] as List<dynamic>);
(result.data!['search']['nodes'] as List<dynamic>);

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) {
// this is where you combine your previous data and response
// 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<dynamic>,
...fetchMoreResultData['search']['nodes'] as List<dynamic>
...previousResultData!['search']['nodes']
as List<dynamic>,
...fetchMoreResultData!['search']['nodes']
as List<dynamic>
];

// to avoid alot of work, lets just update the list of repos in returned
Expand Down Expand Up @@ -155,7 +157,7 @@ class _MyHomePageState extends State<MyHomePage> {
style: Theme.of(context).textTheme.caption),
ElevatedButton(
onPressed: () {
fetchMore(opts);
fetchMore!(opts);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
36 changes: 18 additions & 18 deletions packages/graphql_flutter/example/lib/graphql_bloc/bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<List<Repo>> _repoSubject =
BehaviorSubject<List<Repo>>();
Stream<List<Repo>> get repoStream => _repoSubject.stream;
final BehaviorSubject<List<Repo>?> _repoSubject =
BehaviorSubject<List<Repo>?>();
Stream<List<Repo>?> get repoStream => _repoSubject.stream;

final ReplaySubject<Repo> _toggleStarSubject = ReplaySubject<Repo>();
Sink<Repo> get toggleStarSink => _toggleStarSubject;

/// The repo currently loading, if any
final BehaviorSubject<String> _toggleStarLoadingSubject =
BehaviorSubject<String>();
final BehaviorSubject<String?> _toggleStarLoadingSubject =
BehaviorSubject<String?>();

Stream<String> get toggleStarLoadingStream =>
Stream<String?> get toggleStarLoadingStream =>
_toggleStarLoadingSubject.stream;

final BehaviorSubject<int> _updateNumberOfRepo = BehaviorSubject<int>();
Expand All @@ -71,8 +71,8 @@ class Bloc {
Future<QueryResult> _mutateToggleStar(Repo repo) async {
final _options = MutationOptions(
document:
gql(repo.viewerHasStarred ? mutations.removeStar : mutations.addStar),
variables: <String, String>{
gql(repo.viewerHasStarred! ? mutations.removeStar : mutations.addStar),
variables: <String, String?>{
'starrableId': repo.id,
},
);
Expand All @@ -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<dynamic>] or a [Map<String, dynamic>]
final repositories =
result.data['viewer']['repositories']['nodes'] as List<dynamic>;
result.data!['viewer']['repositories']['nodes'] as List<dynamic>;

_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());
}
Expand Down
30 changes: 15 additions & 15 deletions packages/graphql_flutter/example/lib/graphql_bloc/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -40,10 +40,10 @@ class _MyHomePageState extends State<GraphQLBlocPatternScreen> {
onChanged: (String n) =>
bloc.updateNumberOfRepoSink.add(int.parse(n)),
),
StreamBuilder<List<Repo>>(
StreamBuilder<List<Repo>?>(
stream: bloc.repoStream,
builder:
(BuildContext context, AsyncSnapshot<List<Repo>> snapshot) {
(BuildContext context, AsyncSnapshot<List<Repo>?> snapshot) {
if (snapshot.hasError) {
return Text('\nErrors: \n ' +
(snapshot.error as List<dynamic>).join(',\n '));
Expand All @@ -54,7 +54,7 @@ class _MyHomePageState extends State<GraphQLBlocPatternScreen> {
);
}

final repositories = snapshot.data;
final repositories = snapshot.data!;

return Expanded(
child: ListView.builder(
Expand All @@ -75,42 +75,42 @@ class _MyHomePageState extends State<GraphQLBlocPatternScreen> {

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<String, Object> extractRepositoryData(Map<String, Object> data) {
final action = data['action'] as Map<String, Object>;
Map<String, Object>? extractRepositoryData(Map<String, Object> data) {
final action = data['action'] as Map<String, Object>?;

if (action == null) {
return null;
}

return action['starrable'] as Map<String, Object>;
return action['starrable'] as Map<String, Object>?;
}

bool get viewerHasStarred => repository.viewerHasStarred;
bool? get viewerHasStarred => repository.viewerHasStarred;

@override
Widget build(BuildContext context) {
return StreamBuilder<String>(
return StreamBuilder<String?>(
stream: bloc.toggleStarLoadingStream,
initialData: null,
builder: (BuildContext context, AsyncSnapshot<String> result) {
builder: (BuildContext context, AsyncSnapshot<String?> 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);
},
Expand Down
Loading

0 comments on commit fe69bce

Please # to comment.