简体   繁体   English

未为类型“_WebViewTabState”定义方法“_requestDownload”

[英]The method '_requestDownload' isn't defined for the type '_WebViewTabState'

I am trying to use flutter_downloader for downloads within InAppWebView.我正在尝试使用 flutter_downloader 在 InAppWebView 中进行下载。 I am trying to use onDownloadStartRequest function of InAppWebViewController to access flutter_downloader download task function, _requestDownload from another file.我正在尝试使用InAppWebViewControlleronDownloadStartRequest function 从另一个文件访问flutter_downloader下载任务 function, _requestDownload But I'm getting this error但是我收到了这个错误

The method '_requestDownload' isn't defined for the type '_WebViewTabState'.方法“_requestDownload”没有为类型“_WebViewTabState”定义。 (Documentation) Try correcting the name to the name of an existing method, or defining a method named '_requestDownload'. (文档)尝试将名称更正为现有方法的名称,或定义名为“_requestDownload”的方法。

How can I use global key to access that method from _WebViewTabState?如何使用全局键从 _WebViewTabState 访问该方法?

webview_tab.dart where I'am trying to access _requestDownload function: webview_tab.dart 我正在尝试访问 _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. downloads_page.dart 其中定义了 _requestDownload() function。

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) {
  }
}
  

It seems you are trying to call a Private method from outside the library where it is defined.您似乎正试图从定义它的库外部调用 Private 方法。

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!Flutter中,前缀_将方法声明为Private ,这意味着您无法从定义它的库外部访问它!

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!此外,要从另一个库调用requestDownload ,您必须在_DownloadsPageState之外(在根级别)声明它,因为_DownloadsPageState本身就是私有的!

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 .因此,为了在webview_tab.dart中使用requestDownload ,您需要导入downloads_page.dart


To learn more on this topic, please visit the Dart's Language tour .要了解有关此主题的更多信息,请访问Dart 的语言之旅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dart:没有为“Type”类型定义方法 - Dart: method isn't defined for the type 'Type' 没有为类型“type”定义方法“fromjson” - the method 'fromjson' isn't defined for the type 'type' 方法“getContacts”没有为类型“Type”定义 - The method 'getContacts' isn't defined for the type 'Type' 方法 &#39;placemarkFromCoordinates&#39; 没有为类型 &#39;Geolocator&#39; 定义 - The method 'placemarkFromCoordinates' isn't defined for the type 'Geolocator' 未为“BuildContext”类型定义方法“bloc” - The method 'bloc' isn't defined for the type 'BuildContext' "没有为“FadeAnimation”类型定义方法“MultiTrackTween”" - The method 'MultiTrackTween' isn't defined for the type 'FadeAnimation' 没有为“FirebaseAuth”类型定义方法“signInWithAnonymously” - The method 'signInWithAnonymously' isn't defined for the type 'FirebaseAuth' 没有为 FirebaseStorage 类型定义方法“collection” - The method "collection" isn't defined for the type FirebaseStorage 没有为“PickedFile”类型定义“复制”方法 - The method 'copy' isn't defined for the type 'PickedFile' 没有为“LogoState”类型定义方法“ControlledAnimation” - The method 'ControlledAnimation' isn't defined for the type 'LogoState'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM