flutterdartflutter-inappwebviewflutter-downloader

The method '_requestDownload' isn't defined for the type '_WebViewTabState'


I am trying to use flutter_downloader for downloads within InAppWebView. I am trying to use onDownloadStartRequest function of InAppWebViewController to access flutter_downloader download task function, _requestDownload from another file. But I'm getting this error

The method '_requestDownload' isn't defined for the type '_WebViewTabState'. (Documentation) Try correcting the name to the name of an existing method, or defining a method named '_requestDownload'.

How can I use global key to access that method from _WebViewTabState?

webview_tab.dart where I'am trying to access _requestDownload function:

@override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: _buildWebView(),
    );
  }
InAppWebView _buildWebView() {
    var browserModel = Provider.of<BrowserModel>(context, listen: true);
    var settings = browserModel.getSettings();
    var currentWebViewModel = Provider.of<WebViewModel>(context, listen: true);

    if (Util.isAndroid()) {
      InAppWebViewController.setWebContentsDebuggingEnabled(
          settings.debuggingEnabled);
    }
    //----Other code---
    return InAppWebView(
       key: webViewTabStateKey,
      initialUrlRequest: URLRequest(url: widget.webViewModel.url),
      initialSettings: initialSettings,
      onWebViewCreated: (controller) async {//--code}

      onDownloadStartRequest: (controller, downloadStartRequest) async {
        await _requestDownload(TaskInfo()); //ERROR HERE
      },
    ),
}

downloads_page.dart where _requestDownload() function is defined.

class _DownloadsPageState extends State<DownloadsPage> {
  List<TaskInfo>? _tasks;
  late List<ItemHolder> _items;
  late bool _showContent;
  late bool _permissionReady;
  late bool _saveInPublicStorage;
  late String _localPath;
  final ReceivePort _port = ReceivePort();
  //Isolate Code

  //Download Request
  Future<void> _requestDownload(TaskInfo task) async {
    var hasStoragePermission = await Permission.storage.isGranted;
    if (!hasStoragePermission) {
      final status = await Permission.storage.request();
      hasStoragePermission = status.isGranted;
    }
    if (hasStoragePermission) {
      task.taskId = await FlutterDownloader.enqueue(
        url: task.link!,
        headers: {'auth': 'test_for_sql_encoding'},
        savedDir: _localPath,
        saveInPublicStorage: _saveInPublicStorage,
      );
    }
  }
  @override
  Widget build(BuildContext context) {
  }
}
  

Solution

  • It seems you are trying to call a Private method from outside the library where it is defined.

    Let's fix that!


    First remove the prefix _ from the method name that is supposed to be accessed from outside the library where it is defined.

    I mean, instead of declaring:

    Future<void> _requestDownload(TaskInfo task) async {
    ...
    

    if you want to be able to call that method from outside you have to define it as:

    Future<void> requestDownload(TaskInfo task) async {
    ...
    

    In Flutter, the prefix _ declares a method as Private, which means you can not access it from outside the library that defines it!

    Additionally, to call requestDownload from another library you will have to declare it outside of _DownloadsPageState (at the root level) because _DownloadsPageState is Private itself!

    class _DownloadsPageState extends State<DownloadsPage> {
    ...
    }
    
    Future<void> requestDownload(TaskInfo task) async {
    ...
    }
    
    

    Finally you will need to import the file where the method is defined in the library you want to use it. So, in order to use requestDownload in webview_tab.dart, there you will need to import downloads_page.dart.


    To learn more on this topic, please visit the Dart's Language tour.