Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type 'Future<dynamic>' is not a subtype of type '() => void'

I am trying to open a URL on a Text click. For this I am using InkWell as shown below:

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

Using this I am getting following error:

════════ 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
like image 740
Code Hunter Avatar asked Sep 12 '19 08:09

Code Hunter


2 Answers

Your launchURL(blogModel.url) call returns Future, and onTap needs a void.

There are 2 solutions to fix this problem.

  1. onTap: () => launchURL(blogModel.url),
    
  2. onTap: () {
      launchURL(blogModel.url); // here you can also use async-await
    }
    
like image 91
CopsOnRoad Avatar answered Nov 15 '22 12:11

CopsOnRoad


It's exactly how cops solve it; if you are calling a method instead you should construct it like this:

before onPressed: authBloc.logout()

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

like image 5
Cristian Cruz Avatar answered Nov 15 '22 10:11

Cristian Cruz