类型 'Future<dynamic>' 不是类型 '() => void' 的子类型

Cod*_*ter 24 dart flutter flutter-layout

我正在尝试Text单击打开一个 URL 。为此,我使用InkWell如下所示:

Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: <Widget>[
       Text('${blogModel.timeElapsed} ago'),
       InkWell(
          child: Text('Read'),
          onTap: launchURL(blogModel.url),
       )
     ],
  )
Run Code Online (Sandbox Code Playgroud)

使用这个我收到以下错误:

???????? Exception caught by widgets library ???????????????????????????????????????????????????????
The following assertion was thrown building BlogTileWidget(dirty):
type 'Future<dynamic>' is not a subtype of type '() => void'

Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md
Run Code Online (Sandbox Code Playgroud)

Cop*_*oad 68

您的launchURL(blogModel.url)电话返回Future,并且onTap需要一个void.

有 2 种解决方案可以解决此问题。

  1. onTap: () => launchURL(blogModel.url),
    
    Run Code Online (Sandbox Code Playgroud)
  2. onTap: () {
      launchURL(blogModel.url); // here you can also use async-await
    }
    
    Run Code Online (Sandbox Code Playgroud)


小智 6

这正是警察解决问题的方法;如果你正在调用一个方法,你应该像这样构造它:

onPressed: authBloc.logout()

后: onPressed: ()=>authBloc.logout()