From 5b3bc45db5ed6efcf123ea7888e6c2ae252eeaf5 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Mon, 14 Jun 2021 14:56:42 +0800 Subject: [PATCH] refactor: fix null types --- lib/models/auth.dart | 14 ++-- lib/screens/gh_commits.dart | 2 +- lib/screens/gh_compare.dart | 2 +- lib/screens/gh_contributors.dart | 2 +- lib/screens/gh_events.dart | 2 +- lib/screens/gh_files.dart | 2 +- lib/screens/gh_gists.dart | 2 +- lib/screens/gh_gists_files.dart | 2 +- lib/screens/gh_issue.dart | 4 +- lib/screens/gh_issue_form.dart | 2 +- lib/screens/gh_issues.dart | 2 +- lib/screens/gh_news.dart | 4 +- lib/screens/gh_notification.dart | 4 +- lib/screens/gh_object.dart | 2 +- lib/screens/gh_org_repos.dart | 2 +- lib/screens/gh_orgs.dart | 2 +- lib/screens/gh_pulls.dart | 2 +- lib/screens/gh_releases.dart | 2 +- lib/screens/gh_repo.dart | 12 ++-- lib/screens/gh_repos.dart | 4 +- lib/screens/gh_user.dart | 8 +-- lib/screens/gh_users.dart | 10 +-- lib/screens/go_user.dart | 102 ++++++++++++++--------------- lib/widgets/notification_item.dart | 2 +- 24 files changed, 90 insertions(+), 102 deletions(-) diff --git a/lib/models/auth.dart b/lib/models/auth.dart index 54dc676..330fc27 100644 --- a/lib/models/auth.dart +++ b/lib/models/auth.dart @@ -688,18 +688,15 @@ class AuthModel with ChangeNotifier { // var _timeoutDuration = Duration(seconds: 1); GitHub? _ghClient; - GitHub? get ghClient { - if (token == null) return null; + GitHub get ghClient { if (_ghClient == null) { _ghClient = GitHub(auth: Authentication.withToken(token)); } - return _ghClient; + return _ghClient!; } Client? _gqlClient; - Client? get gqlClient { - if (token == null) return null; - + Client get gqlClient { if (_gqlClient == null) { _gqlClient = Client( link: HttpLink( @@ -710,16 +707,13 @@ class AuthModel with ChangeNotifier { ); } - return _gqlClient; + return _gqlClient!; } Future query(String query, [String? _token]) async { if (_token == null) { _token = token; } - if (_token == null) { - throw 'token is null'; - } final res = await http .post(Uri.parse(_apiPrefix + '/graphql'), diff --git a/lib/screens/gh_commits.dart b/lib/screens/gh_commits.dart index eb1745d..1a516ea 100644 --- a/lib/screens/gh_commits.dart +++ b/lib/screens/gh_commits.dart @@ -43,7 +43,7 @@ class GhCommits extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; final ref = res.data!.repository!.defaultBranchRef ?? res.data!.repository!.ref!; final history = (ref.target as GCommitsRefCommit).history; diff --git a/lib/screens/gh_compare.dart b/lib/screens/gh_compare.dart index c74df18..02aa557 100644 --- a/lib/screens/gh_compare.dart +++ b/lib/screens/gh_compare.dart @@ -20,7 +20,7 @@ class GhComparisonScreen extends StatelessWidget { return RefreshStatefulScaffold( title: AppBarTitle(AppLocalizations.of(context)!.files), fetch: () async { - final res = await context.read().ghClient!.getJSON( + final res = await context.read().ghClient.getJSON( '/repos/$owner/$name/compare/$before...$head', convert: (dynamic vs) => GithubComparisonItem.fromJson(vs)); return res.files; diff --git a/lib/screens/gh_contributors.dart b/lib/screens/gh_contributors.dart index 842d6e2..d54595c 100644 --- a/lib/screens/gh_contributors.dart +++ b/lib/screens/gh_contributors.dart @@ -20,7 +20,7 @@ class GhContributorsScreen extends StatelessWidget { page = page ?? 1; final res = await context .read() - .ghClient! + .ghClient .getJSON>( '/repos/$owner/$name/contributors?page=$page', convert: (vs) => diff --git a/lib/screens/gh_events.dart b/lib/screens/gh_events.dart index a40a51a..4ded93d 100644 --- a/lib/screens/gh_events.dart +++ b/lib/screens/gh_events.dart @@ -20,7 +20,7 @@ class GhEventsScreen extends StatelessWidget { itemBuilder: (payload) => EventItem(payload), fetch: (page) async { page = page ?? 1; - final events = await context.read().ghClient!.getJSON( + final events = await context.read().ghClient.getJSON( '/users/$login/events?page=$page&per_page=$PAGE_SIZE', convert: (dynamic vs) => [for (var v in vs) GithubEvent.fromJson(v)]); diff --git a/lib/screens/gh_files.dart b/lib/screens/gh_files.dart index 1b7f9ba..ac7bdc8 100644 --- a/lib/screens/gh_files.dart +++ b/lib/screens/gh_files.dart @@ -31,7 +31,7 @@ class GhFilesScreen extends StatelessWidget { page = page ?? 1; final res = await context .read() - .ghClient! + .ghClient .getJSON>( '/repos/$owner/$name/pulls/$pullNumber/files?page=$page', convert: (vs) => [for (var v in vs) GithubFilesItem.fromJson(v)], diff --git a/lib/screens/gh_gists.dart b/lib/screens/gh_gists.dart index 6ab2c50..2a7ae9c 100644 --- a/lib/screens/gh_gists.dart +++ b/lib/screens/gh_gists.dart @@ -24,7 +24,7 @@ class GhGistsScreen extends StatelessWidget { ..vars.login = login ..vars.after = page); final OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; final gists = res.data!.user!.gists; return ListPayload( cursor: gists.pageInfo.endCursor, diff --git a/lib/screens/gh_gists_files.dart b/lib/screens/gh_gists_files.dart index cc88c9c..9e3b1e7 100644 --- a/lib/screens/gh_gists_files.dart +++ b/lib/screens/gh_gists_files.dart @@ -26,7 +26,7 @@ class GhGistsFilesScreen extends StatelessWidget { ..vars.login = login ..vars.name = id); final OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; final gist = res.data!.user!.gist; return gist; }, diff --git a/lib/screens/gh_issue.dart b/lib/screens/gh_issue.dart index 8e0d97e..ad6e4c5 100644 --- a/lib/screens/gh_issue.dart +++ b/lib/screens/gh_issue.dart @@ -96,7 +96,7 @@ class GhIssueScreen extends StatelessWidget { b.vars.cursor = cursor; }); OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; return res.data!.repository!; } @@ -115,7 +115,7 @@ class GhIssueScreen extends StatelessWidget { ActionItem( text: d.closed ? 'Reopen issue' : 'Close issue', onTap: (_) async { - await context.read().ghClient!.issues.edit( + await context.read().ghClient.issues.edit( github.RepositorySlug(owner, name), number, github.IssueRequest( diff --git a/lib/screens/gh_issue_form.dart b/lib/screens/gh_issue_form.dart index 82d779b..c91c170 100644 --- a/lib/screens/gh_issue_form.dart +++ b/lib/screens/gh_issue_form.dart @@ -59,7 +59,7 @@ class _GhIssueFormScreenState extends State { final slug = RepositorySlug(widget.owner, widget.name); final res = await context .read() - .ghClient! + .ghClient .issues .create(slug, IssueRequest(title: _title, body: _body)); await theme.push( diff --git a/lib/screens/gh_issues.dart b/lib/screens/gh_issues.dart index f33fa97..698cd8a 100644 --- a/lib/screens/gh_issues.dart +++ b/lib/screens/gh_issues.dart @@ -33,7 +33,7 @@ class GhIssuesScreen extends StatelessWidget { b.vars.cursor = cursor; }); final OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; final issues = res.data!.repository!.issues; return ListPayload( cursor: issues.pageInfo.endCursor, diff --git a/lib/screens/gh_news.dart b/lib/screens/gh_news.dart index b2a7230..e7ffc1a 100644 --- a/lib/screens/gh_news.dart +++ b/lib/screens/gh_news.dart @@ -24,7 +24,7 @@ class GhNewsScreenState extends State { // 1 item is enough since count is not displayed for now. var items = await context .read() - .ghClient! + .ghClient .getJSON('/notifications?per_page=1'); if (items is List && items.isNotEmpty) { @@ -43,7 +43,7 @@ class GhNewsScreenState extends State { final auth = context.read(); final login = auth.activeAccount!.login; - final events = await auth.ghClient!.getJSON( + final events = await auth.ghClient.getJSON( '/users/$login/received_events?page=$page&per_page=$PAGE_SIZE', convert: (dynamic vs) => [for (var v in vs) GithubEvent.fromJson(v)], ); diff --git a/lib/screens/gh_notification.dart b/lib/screens/gh_notification.dart index d2756af..4d07edf 100644 --- a/lib/screens/gh_notification.dart +++ b/lib/screens/gh_notification.dart @@ -21,7 +21,7 @@ class GhNotificationScreen extends StatefulWidget { class GhNotificationScreenState extends State { Future> fetchNotifications(int index) async { - final ns = await context.read().ghClient!.getJSON( + final ns = await context.read().ghClient.getJSON( '/notifications?all=${index == 2}&participating=${index == 1}', convert: (dynamic vs) => [for (var v in vs) GithubNotificationItem.fromJson(v)], @@ -123,7 +123,7 @@ ${item.key}: pullRequest(number: ${item.subject!.number}) { onTap: () async { await context .read() - .ghClient! + .ghClient .activity .markRepositoryNotificationsRead( RepositorySlug.full(group.fullName!)); diff --git a/lib/screens/gh_object.dart b/lib/screens/gh_object.dart index 98f7f48..84f3771 100644 --- a/lib/screens/gh_object.dart +++ b/lib/screens/gh_object.dart @@ -37,7 +37,7 @@ class GhObjectScreen extends StatelessWidget { final suffix = path == null ? '' : '/$path'; final res = await context .read() - .ghClient! + .ghClient .repositories .getContents(RepositorySlug(owner, name), suffix, ref: ref); if (res.isDirectory) { diff --git a/lib/screens/gh_org_repos.dart b/lib/screens/gh_org_repos.dart index 2910b29..fe1b71e 100644 --- a/lib/screens/gh_org_repos.dart +++ b/lib/screens/gh_org_repos.dart @@ -25,7 +25,7 @@ class GhOrgReposScreen extends StatelessWidget { page = page ?? 1; final rs = await context .read() - .ghClient! + .ghClient .getJSON>( '/orgs/$owner/repos?sort=updated&page=$page', convert: (vs) => [for (var v in vs) Repository.fromJson(v)], diff --git a/lib/screens/gh_orgs.dart b/lib/screens/gh_orgs.dart index 53e01ae..d40974b 100644 --- a/lib/screens/gh_orgs.dart +++ b/lib/screens/gh_orgs.dart @@ -19,7 +19,7 @@ class GhUserOrganizationScreen extends StatelessWidget { page = page ?? 1; final res = await context .read() - .ghClient! + .ghClient .getJSON>( '/users/$login/orgs?page=$page', convert: (vs) => diff --git a/lib/screens/gh_pulls.dart b/lib/screens/gh_pulls.dart index 8400247..049d036 100644 --- a/lib/screens/gh_pulls.dart +++ b/lib/screens/gh_pulls.dart @@ -28,7 +28,7 @@ class GhPullsScreen extends StatelessWidget { b.vars.cursor = cursor; }); final OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; final pulls = res.data!.repository!.pullRequests; return ListPayload( cursor: pulls.pageInfo.endCursor, diff --git a/lib/screens/gh_releases.dart b/lib/screens/gh_releases.dart index 40996be..397cf2c 100644 --- a/lib/screens/gh_releases.dart +++ b/lib/screens/gh_releases.dart @@ -26,7 +26,7 @@ class GhReleasesScreen extends StatelessWidget { ..vars.name = name ..vars.cursor = page); final OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; final releases = res.data!.repository!.releases; return ListPayload( cursor: releases.pageInfo.endCursor, diff --git a/lib/screens/gh_repo.dart b/lib/screens/gh_repo.dart index 11d86db..04a4f43 100644 --- a/lib/screens/gh_repo.dart +++ b/lib/screens/gh_repo.dart @@ -57,10 +57,10 @@ class GhRepoScreen extends StatelessWidget { ..vars.branchSpecified = branch != null ..vars.branch = branch ?? ''); final OperationResponse res = - await context.read().gqlClient!.request(req).first; + await context.read().gqlClient.request(req).first; final repo = res.data!.repository; - final ghClient = context.read().ghClient!; + final ghClient = context.read().ghClient; final countFuture = ghClient .getJSON('/repos/$owner/$name/stats/contributors') .then((v) => (v as List).length); @@ -132,10 +132,8 @@ class GhRepoScreen extends StatelessWidget { ActionItem( text: _buildWatchState(v), onTap: (_) async { - final activityApi = context - .read() - .ghClient! - .activity; + final activityApi = + context.read().ghClient.activity; switch (v) { case GSubscriptionState.SUBSCRIBED: case GSubscriptionState.IGNORED: @@ -173,7 +171,7 @@ class GhRepoScreen extends StatelessWidget { text: repo.viewerHasStarred ? 'Unstar' : 'Star', onTap: () async { final activityApi = - context.read().ghClient!.activity; + context.read().ghClient.activity; if (repo.viewerHasStarred) { await activityApi.unstar( RepositorySlug(repo.owner.login, repo.name)); diff --git a/lib/screens/gh_repos.dart b/lib/screens/gh_repos.dart index cd008fc..26dd98d 100644 --- a/lib/screens/gh_repos.dart +++ b/lib/screens/gh_repos.dart @@ -25,7 +25,7 @@ class GhRepos extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; final p = res.data!.user!.repositories; return ListPayload( cursor: p.pageInfo.endCursor, @@ -56,7 +56,7 @@ class GhStars extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; final p = res.data!.user!.starredRepositories; return ListPayload( cursor: p.pageInfo.endCursor, diff --git a/lib/screens/gh_user.dart b/lib/screens/gh_user.dart index df54ba5..d57c516 100644 --- a/lib/screens/gh_user.dart +++ b/lib/screens/gh_user.dart @@ -272,7 +272,7 @@ class GhViewer extends StatelessWidget { fetch: () async { final req = GViewerReq(); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; return res.data!.viewer; }, title: AppBarTitle(AppLocalizations.of(context)!.me), @@ -298,7 +298,7 @@ class GhUser extends StatelessWidget { fetch: () async { final req = GUserReq((b) => b..vars.login = login); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; return res.data; }, title: AppBarTitle(login), @@ -322,9 +322,9 @@ class GhUser extends StatelessWidget { : AppLocalizations.of(context)!.follow, onTap: () async { if (p.viewerIsFollowing) { - await auth.ghClient!.users.unfollowUser(p.login); + await auth.ghClient.users.unfollowUser(p.login); } else { - await auth.ghClient!.users.followUser(p.login); + await auth.ghClient.users.followUser(p.login); } setData(data.rebuild((b) { final u = b.repositoryOwner diff --git a/lib/screens/gh_users.dart b/lib/screens/gh_users.dart index ec2bee1..34a4c26 100644 --- a/lib/screens/gh_users.dart +++ b/lib/screens/gh_users.dart @@ -24,7 +24,7 @@ class GhFollowers extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; final p = res.data!.user!.followers; return ListPayload( cursor: p.pageInfo.endCursor, @@ -54,7 +54,7 @@ class GhFollowing extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; final p = res.data!.user!.following; return ListPayload( cursor: p.pageInfo.endCursor, @@ -84,7 +84,7 @@ class GhMembers extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; final p = res.data!.organization!.membersWithRole; return ListPayload( cursor: p.pageInfo.endCursor, @@ -116,7 +116,7 @@ class GhWachers extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; final p = res.data!.repository!.watchers; return ListPayload( cursor: p.pageInfo.endCursor, @@ -148,7 +148,7 @@ class GhStargazers extends StatelessWidget { b.vars.after = cursor; }); final OperationResponse res = - await auth.gqlClient!.request(req).first; + await auth.gqlClient.request(req).first; final p = res.data!.repository!.stargazers; return ListPayload( cursor: p.pageInfo.endCursor, diff --git a/lib/screens/go_user.dart b/lib/screens/go_user.dart index 1803876..13da73e 100644 --- a/lib/screens/go_user.dart +++ b/lib/screens/go_user.dart @@ -44,62 +44,58 @@ class GoUserScreen extends StatelessWidget { bodyBuilder: (p, _) { final user = p.item1; final repos = p.item2; - if (p.item1 != null) { - return Column( - children: [ - UserHeader( - login: user.username, - avatarUrl: user.avatarUrl, - name: user.fullName, - createdAt: - null, // TODO: API response does not have this attribute - isViewer: isViewer, - bio: null, // TODO: API response does not have this attribute + return Column( + children: [ + UserHeader( + login: user.username, + avatarUrl: user.avatarUrl, + name: user.fullName, + createdAt: + null, // TODO: API response does not have this attribute + isViewer: isViewer, + bio: null, // TODO: API response does not have this attribute + ), + CommonStyle.border, + Row(children: [ + EntryItem( + text: 'Repositories', + url: '/gogs/$login?tab=repositories&isViewer=$isViewer', ), - CommonStyle.border, - Row(children: [ - EntryItem( - text: 'Repositories', - url: '/gogs/$login?tab=repositories&isViewer=$isViewer', + EntryItem( + text: 'Followers', + url: '/gogs/$login?tab=followers', + ), + EntryItem( + text: 'Following', + url: '/gogs/$login?tab=following', + ), + ]), + CommonStyle.border, + TableView( + items: [ + TableViewItem( + leftIconData: Octicons.home, + text: Text('Organizations'), + url: + '/gogs/${user.username}?tab=organizations&isViewer=$isViewer', ), - EntryItem( - text: 'Followers', - url: '/gogs/$login?tab=followers', - ), - EntryItem( - text: 'Following', - url: '/gogs/$login?tab=following', - ), - ]), - CommonStyle.border, - TableView( - items: [ - TableViewItem( - leftIconData: Octicons.home, - text: Text('Organizations'), - url: - '/gogs/${user.username}?tab=organizations&isViewer=$isViewer', + ], + ), + CommonStyle.border, + Column( + children: [ + for (var v in repos) ...[ + RepositoryItem.go( + payload: v, + name: v.fullName!.split('/')[1], + owner: v.owner!.username, ), - ], - ), - CommonStyle.border, - Column( - children: [ - for (var v in repos) ...[ - RepositoryItem.go( - payload: v, - name: v.fullName!.split('/')[1], - owner: v.owner!.username, - ), - CommonStyle.border, - ] - ], - ), - ], - ); - } else { - return Text('404'); // TODO: - } + CommonStyle.border, + ] + ], + ), + ], + ); }, ); } diff --git a/lib/widgets/notification_item.dart b/lib/widgets/notification_item.dart index 967df16..2cdc4a7 100644 --- a/lib/widgets/notification_item.dart +++ b/lib/widgets/notification_item.dart @@ -83,7 +83,7 @@ class _NotificationItemState extends State { try { await context .read() - .ghClient! + .ghClient .activity .markThreadRead(payload.id!); widget.markAsRead();