Returns a Future<Dynamic> instead of Bool

1,192

Solution 1

Return bool using Future object

return Future<bool>.value(true);

and modify method like

Future<bool> cla() async{

Use like:

 cla().then((value) {
      // value will provide you true or false
    });

Solution 2

Future<bool> cla() async {
    bool d=false;
    try {
      final result = await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        print('connected');
        d= true;
      }
    } on SocketException catch (_) {
      print('not connected');
      d= false;
    }
   return d;
}

@override
void initState() {
  super.initState();
  // calling cla function 
  setState(() async {
  var value = await getWeather();
  if(value){
   ...
  }
  });
}

The observation you found is expected. Since the operation inside cla() function is async and the method is marked as async to return the future result. So you. will get future and to get the result form future you have to call await on it as shown above.

Solution 3

Future<bool> cla() async{
 // function body goes here..
}

you can explicitly define return type of the method.

Share:
1,192
Thaanu
Author by

Thaanu

Updated on December 20, 2022

Comments

  • Thaanu
    Thaanu over 1 year

    I want to return a bool in this method but it return a Future

    @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(left: 0, right: 0, top: 110, bottom: 5),
                  child: SingleChildScrollView(
                    child: Column(
                      children: <Widget>[
                        QuestionCards(),
                        cla().then((value) => { //this can't be add like this
                          YoutubeViewer(
                              videoId: 'hVQUbKs6qN8', topic: 'topic1'),
                        }
                      )
                   ],
                ).
              ),
            ],
          ),
        );
      }
    
      Future<bool> cla() async {
        bool d = false;
        try {
          final result = await InternetAddress.lookup('google.com');
          if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
            print('connected');
            return Future<bool>.value(true);
          }
        } on SocketException catch (_) {
          print('not connected');
          return Future<bool>.value(false);
        }
      }
    

    If someone can tell me that what need to be changed in this It would be really helpful Thank you

  • Thaanu
    Thaanu almost 4 years
    I want the output in a if statement but when i do this it says condition must have a static type of 'bool'
  • Thaanu
    Thaanu almost 4 years
    I want the output in a if statement but how can I add this with if statement?
  • Thaanu
    Thaanu almost 4 years
    I want the output in a if statement but when i do this it says condition must have a static type of 'bool'
  • Thaanu
    Thaanu almost 4 years
    thanks but this can be used inside a widget right? it says 'can't be assigned to a list type Widget'
  • Jitesh Mohite
    Jitesh Mohite almost 4 years
    @Thaanu: Can you edit your question and tell me how you are using there
  • Jitesh Mohite
    Jitesh Mohite almost 4 years
    @Thaanu: I can see you are not using the value variable in YoutubeViewer, Actually value is of boolean type, you can use if condition with it like if(value) {} else {}
  • Thaanu
    Thaanu almost 4 years
    Problem is "cla().then((value)" part can't add to there, it gives 'can't be assigned to a list type Widget'
  • Jitesh Mohite
    Jitesh Mohite almost 4 years