shirsh94 / flutter_doc_scanner

MIT License
5 stars 6 forks source link

Use a predefined object type for documents result instead of using dynamic return type #11

Closed UnluckyY1 closed 1 week ago

UnluckyY1 commented 3 months ago

In Dart, using dynamic as a return type is generally considered poor practice. It undermines the type safety provided by the language and can lead to runtime errors that could have been caught at compile time. It's recommended to specify the return type explicitly to make your code easier to understand, maintain, and debug...

I've reviewed the Android portion of the code and noticed that it returns a map containing a Uri for the file and a page count. While I'm uncertain about the return type used in the iOS section, it should be relatively straightforward to establish a universal return type within the Dart codebase

This can be done simply by creating a simple data class that can deserialize JSON objects:

class DocumentsPdfResult {
  Uri fileUri;
  int pageCount;

  DocumentsPdfResult(this.fileUri, this.pageCount);

  factory DocumentsPdfResult.fromJson(Map<String, dynamic> json) {
    return DocumentsPdfResult(
      Uri.parse(json['fileUri']),
      json['pageCount'] as int,
    );
  }
}

and then inside : flutter_doc_scanner_method_channel.dart:

@override
  Future<DocumentsPdfResult> getScanDocuments() async {
    final data = await methodChannel.invokeMethod<dynamic>('getScanDocuments');
    return DocumentsPdfResult.fromJson(data);
  }
shirsh94 commented 1 week ago

Hi,

Thank you for your feedback on using dynamic as a return type. While I understand the benefits of specifying explicit return types, in this case, dynamic was used to maintain flexibility across different platform implementations and handle various data structures.

The current implementation allows for easier integration with the platform channels without tightly coupling to a specific data model. This approach is intended to accommodate potential variations in the data format returned by the native code.

If you have suggestions on how to better balance flexibility and type safety while using dynamic, I'd be happy to consider them. Your input is valuable for improving the library.

Thanks for your understanding!

UnluckyY1 commented 1 week ago

I will try to create a PR soon with my suggestions. :)