Use firstWhereOrNull

Simplifies the code
This commit is contained in:
Vishesh Handa
2021-05-25 10:41:41 +02:00
parent 4632dc40b9
commit f133550921
3 changed files with 8 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
import 'package:dart_git/config.dart'; import 'package:dart_git/config.dart';
import 'package:dart_git/dart_git.dart'; import 'package:dart_git/dart_git.dart';
import 'package:git_bindings/git_bindings.dart'; import 'package:git_bindings/git_bindings.dart';
@ -544,8 +545,8 @@ class GitJournalRepo with ChangeNotifier {
} }
var remoteConfig = repo.config.remotes.first; var remoteConfig = repo.config.remotes.first;
var remoteBranches = await repo.remoteBranches(remoteConfig.name); var remoteBranches = await repo.remoteBranches(remoteConfig.name);
var remoteBranchRef = remoteBranches var remoteBranchRef =
.firstWhere((ref) => ref.name.branchName() == name, orElse: null); remoteBranches.firstWhereOrNull((ref) => ref.name.branchName() == name);
if (remoteBranchRef == null) { if (remoteBranchRef == null) {
return ""; return "";
} }
@ -579,9 +580,8 @@ Future<void> _addFileInRepo({
}) async { }) async {
var repoPath = repo.repoPath; var repoPath = repo.repoPath;
var dirList = await Directory(repoPath).list().toList(); var dirList = await Directory(repoPath).list().toList();
var anyFileInRepo = dirList.firstWhere( var anyFileInRepo = dirList.firstWhereOrNull(
(fs) => fs.statSync().type == FileSystemEntityType.file, (fs) => fs.statSync().type == FileSystemEntityType.file,
orElse: () => null,
); );
if (anyFileInRepo == null) { if (anyFileInRepo == null) {
Log.i("Adding .ignore file"); Log.i("Adding .ignore file");

View File

@ -2,6 +2,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:function_types/function_types.dart'; import 'package:function_types/function_types.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
@ -101,10 +102,7 @@ class GitHostSetupRepoSelectorState extends State<GitHostSetupRepoSelector> {
fetchedRepos = true; fetchedRepos = true;
}); });
var repo = repos.firstWhere( var repo = repos.firstWhereOrNull((r) => r.fullName.endsWith('/journal'));
(r) => r.fullName.endsWith('/journal'),
orElse: () => null,
);
if (repo != null) { if (repo != null) {
setState(() { setState(() {
selectedRepo = repo; selectedRepo = repo;

View File

@ -3,6 +3,7 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:gitjournal/core/link.dart'; import 'package:gitjournal/core/link.dart';
@ -45,7 +46,7 @@ class _NoteBacklinkRendererState extends State<NoteBacklinkRenderer> {
var links = await n.fetchLinks(); var links = await n.fetchLinks();
var linkResolver = LinkResolver(n); var linkResolver = LinkResolver(n);
var matchedLink = links.firstWhere( var matchedLink = links.firstWhereOrNull(
(l) { (l) {
var matchedNote = linkResolver.resolveLink(l); var matchedNote = linkResolver.resolveLink(l);
if (matchedNote == null) { if (matchedNote == null) {
@ -54,7 +55,6 @@ class _NoteBacklinkRendererState extends State<NoteBacklinkRenderer> {
return matchedNote.filePath == widget.note.filePath; return matchedNote.filePath == widget.note.filePath;
}, },
orElse: () => null,
); );
// Log.d("NoteBacklinkRenderer Predicate ${matchedLink != null}"); // Log.d("NoteBacklinkRenderer Predicate ${matchedLink != null}");