Usefulness of nullable return types #108
Replies: 3 comments
-
Nah, you're welcome man, thanks for taking time to open this discussion.
You're right, it was a unwise choise I did when I started this package implementation, I'll explain later on.
I agree with you but, though it seems pretty comfortable at the beginning, things start to collapse when implementation starts to get complex.
In the case of this package, this is even more helpful, where we can catch the exception and see why does it failed, and sincerely, this is the best approach, but for some reason we do not have it "as default" inside the Android SDK.
Comically I've write a question on SO about this annoying And though your solution seems to work, as you said, you have not access to the internal implementation to get the exception metadata.
But I think even with exceptions being thrown, we have if + catch statements to handle the error. But this is the advantage here: we have information about the error to handle. // So instead of:
final file = dir.createFile(...);
if (file != null) ...
// We have:
try {
final file = dir.createFile();
} on SecurityException catch(e) {
if (e.restriction == SecurityRestriction.operatingSystem) ...
if (e.restriction == SecurityRestriction.noPermission) ...
} on IOException {
...
}
Yep. Just don't. We do not wanna This should be used just when you know that the type can't be
To be true, this package is already fated to remove the "Mirror APIs" style and starting throwing exceptions as described here github.com/alexrintt/shared-storage/issues/56. For some reasons:
And from what you described seems we're going to the right solution, because these errors (null checking everywhere and missing exception info) would not exists if I did follow this the Flutter recommended article recommendations for creating plugins: https://medium.com/flutter/writing-a-good-flutter-plugin-1a561b986c9c. Let me know if it helps and makes sense. You're welcome to add any missing information and propose other issues and solutions. Thanks. |
Beta Was this translation helpful? Give feedback.
-
So the package is already going this way? thats brilliant, love to hear it. The article also mentions how top level functions are undesirable. |
Beta Was this translation helpful? Give feedback.
-
Yep! This is also being implemented, I've already removed the specific file import statements: import 'package:shared_storage/saf.dart'; // no more
import 'package:shared_storage/media_store.dart'; // no more
import 'package:shared_storage/environment.dart'; // no more
import 'package:shared_storage/shared_storage.dart'; // single import for all above |
Beta Was this translation helpful? Give feedback.
-
First off, wonderful package, thank you for making it.
I know that this package tries to closely mirror the Android SDK API, and therefore follows its documentation.
This is useful because it means we do not need to write documentation again, and people already familiar with it can use it easily.
However, I have noticed that the Android SDK API tends to go a "non-disrupting" route of error-handling:
public abstract @Nullable DocumentFile createDirectory(@NonNull String displayName)
where the return type is described as:
@Nullable DocumentFile file : representing newly created directory, or null if failed
.This is odd to me for two reasons;
null
ornot null
.(I am however not talking about functions like
findFile
wherenull
is returned if nothing is found. that seems natural to me.)It also leads to an incredibly annoying coding style, where after every operation one has to check whether said operation succeeded explicitly, instead of simply assuming it succeeded.
In my own code, I have written a short method to counteract this problem:
That I have simply used on all my usages of SAF:
This solution is not nice, but it is, in my opinion, better than have to write copious amounts of if-statements. Frankly, I am unsure what I would even do inside such an if-statement. Probably throw an exception nonetheless.
I could of course also simply use
!
to always assert that my operations succeed. But this would rob me of the information on why they failed, again.I am not proposing the package should be changed to throw exceptions instead of returning
null
and I doubt that such a change would be implemented upstream in the Android SDK library, but I felt like talking about this because it just seems odd to me.Perhaps I am just doing this the wrong way.
Beta Was this translation helpful? Give feedback.
All reactions