Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: This expression has type 'void' and can't be used. #1

Open
RIP321 opened this issue Aug 3, 2019 · 18 comments
Open

Error: This expression has type 'void' and can't be used. #1

RIP321 opened this issue Aug 3, 2019 · 18 comments

Comments

@RIP321
Copy link

RIP321 commented Aug 3, 2019

void getLocationData() async {
    Location location = Location();
    await location.getCurrentLocation();

    latitude = location.latitude;
    longitude = location.longitude;
    NetworkHelper networkHelper = NetworkHelper(
        url:
            'https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$apiKey&units=metric');

    var weatherData = await networkHelper.getData();
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return LocationScreen(
        locationWeather: weatherData,
      );
    }));
  }


class NetworkHelper {
  NetworkHelper({this.url});

  final String url;
  Future<void> getData() async {
    http.Response response = await http.get(url);
    if (response.statusCode == 200) {
      String data = response.body;
      return jsonDecode(data);
    } else {
      return (response.statusCode);
    }
  }
}

lib/screens/loading_screen.dart:37:26: Error: This expression has type 'void' and can't be used.
locationWeather: weatherData,

In the video tutorial there is no error popping up, but when in my case the error this expression has a void and can't be used is there

Some help, please!

@AdalWay
Copy link

AdalWay commented Sep 16, 2019

Im having issues with same problem...

@RIP321
Copy link
Author

RIP321 commented Sep 17, 2019

Hi AdalWay I've found a solution instead of using future void in class network helper just use future.

As the function is returning a value so we cannot assign future.

I've no idea how did it work in the video of the tutor.

@techno-disaster
Copy link

In NetworkHelper as you are returning something it cant be void

@tamersolieman
Copy link

Hi AdalWay I've found a solution instead of using future void in class network helper just use future.

As the function is returning a value so we cannot assign future.

I've no idea how did it work in the video of the tutor.

This solution works with me.
Thanks @RIP321

@AvinashMahanthi
Copy link

Hi AdalWay I've found a solution instead of using future void in class network helper just use future.

As the function is returning a value so we cannot assign future.

I've no idea how did it work in the video of the tutor.

Yah this also works for me thanks @RIP321

@shaishalevss
Copy link

Same problem... I ran into a different problem when I remove the from my Future function in the networking:
the new problem is:
No constructor 'LocationScreen.' with matching arguments declared in class 'LocationScreen'.
Receiver: LocationScreen
Tried calling: new LocationScreen.()
Found: new LocationScreen.(dynamic, {_Location $creationLocationd_0dea112b090073317d4}) => LocationScreen

@RIP321
Copy link
Author

RIP321 commented Sep 21, 2020

Same problem... I ran into a different problem when I remove the from my Future function in the networking:
the new problem is:
No constructor 'LocationScreen.' with matching arguments declared in class 'LocationScreen'. Receiver: LocationScreen Tried calling: new LocationScreen.() Found: new LocationScreen.(dynamic, {_Location $creationLocationd_0dea112b090073317d4}) => LocationScreen

Share your code

@furquan786
Copy link

i have also same problem "error: This expression has a type of 'void' so its value can't be used. (use_of_void_result at [clima] lib\screens\loading_screen.dart:38)"
please help me...

@AvinashMahanthi
Copy link

i have also same problem "error: This expression has a type of 'void' so its value can't be used. (use_of_void_result at [clima] lib\screens\loading_screen.dart:38)"
please help me...

Please share your code

@FredWolfe
Copy link

Im having issues with same problem...

me too

@AslanDevbrat
Copy link

Hi AdalWay I've found a solution instead of using future void in class network helper just use future.

As the function is returning a value so we cannot assign future.

I've no idea how did it work in the video of the tutor.

That helped me

@malikdanishalii
Copy link

i have also same problem.

@malikdanishalii
Copy link

The error resolved by running the application on real / physical devices because emulator or simulator has the problem of getting real time location as per my knowledge and understanding after working for three days on this issue.

@DeStefaniAndrei
Copy link

Thx so much, to anyone who didn't understand it's the getData function which Angela made us Future, but since it the JsonDecode(data), which we have set weatherData to which we are trying to make it the location screen input, is being returned, so it's only Future, not Future

@AbuTawhidRian
Copy link

I've found a solution instead of using future void in class network helper just use future.

import 'package:http/http.dart' as http;
import 'dart:convert';

class Networking {
Networking(this.url);
final String url;

Future getData() async {
http.Response response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
String data = response.body;
return jsonDecode(data);
} else {
print(response.statusCode);
}
}
}

@AvinandanBose
Copy link

AvinandanBose commented Apr 8, 2022

Hi AdalWay I've found a solution instead of using future void in class network helper just use future.

As the function is returning a value so we cannot assign future.

I've no idea how did it work in the video of the tutor.

Use :

Future<dynamic> getLocation() async {
    Location location = Location();
    await location.getCurrentLocation();
    latitude = location.latitude;
    longitude = location.longitude;
    NetworkHelper networkHelper = NetworkHelper('https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$apiKey');
    var  weatherData = await networkHelper.getData();
    Navigator.push(context, MaterialPageRoute(
      builder: (context){
        return LocationScreen(locationWeather:weatherData,);
    },
    ),
    );
  }

Then:

class NetworkHelper{
  final String url;
  NetworkHelper(this.url);
  Future<dynamic> getData() async {
    http.Response response = await http.get(Uri.parse(url));
    if(response.statusCode == 200) {
      String data = response.body;
      dynamic decodeData = jsonDecode(data);
      return decodeData;
    }
    else{
      print(response.statusCode);
    }

Actually you are using var datatype assigning a void i.e. Future<void> to WeatherData while it will be dynamic . Thats the problem.

@robfrei
Copy link

robfrei commented Nov 17, 2022

Screenshot 2022-11-16 at 7 57 56 PM

@maheenqayyum
Copy link

import 'package:audioplayers/audioplayers.dart';
import 'package:downloader/components/custom_list_tile.dart';
import 'package:flutter/material.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MusicApp(),
);
}
}

class MusicApp extends StatefulWidget {
const MusicApp({Key? key}) : super(key: key);

@OverRide
State createState() => _MusicAppState();
}

class _MusicAppState extends State {

List musicList = [
{
'title': "Tech House Vibes",
'singer': "Alejandro Magana",
'url': "https://assets.mixkit.co/music/preview/mixkit-tech-house-vibes-130.mp3",
'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
},
{
'title': "Hazy After Hours",
'singer': "Alejandro Magana",
'url': "https://assets.mixkit.co/music/preview/mixkit-hazy-after-hours-132.mp3",
'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
},
{
'title': "Hip Hop 02",
'singer': "Lily J",
'url': "https://assets.mixkit.co/music/preview/mixkit-hip-hop-02-738.mp3",
'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
},
{
'title': "A Very Happy Christmas",
'singer': "Michael Ramir C",
'url': "https://assets.mixkit.co/music/preview/mixkit-a-very-happy-christmas-897.mp3",
'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
},
// {
// 'title': "Sun and His Daughter",
// 'singer': "Eugenio Mininni",
// 'url': "https://assets.mixkit.co/music/preview/mixkit-sun-and-his-daughter-580.mp3",
// 'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
// },
// {
// 'title': "Raising Me Higher",
// 'singer': "Ahjay Stelino",
// 'url': "https://assets.mixkit.co/music/preview/mixkit-sun-and-his-daughter-580.mp3",
// 'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
// },
// {
// 'title': "Life is a Dream",
// 'singer': "Michael Ramir C",
// 'url': "https://assets.mixkit.co/music/preview/mixkit-life-is-a-dream-837.mp3",
// 'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
// },
// {
// 'title': "Driving Ambition",
// 'singer': "Ahjay Stelino",
// 'url': "https://assets.mixkit.co/music/preview/mixkit-driving-ambition-32.mp3",
// 'coverUrl': "https://images.unsplash.com/photo-1546521677-b3a9b11bee6f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGNvdmVyfGVufDB8fDB8fHww&auto=format&fit=crop&w=500&q=60"
// },
];

String currentTitle = "";
String currentCover = "";
String currentSinger = "";
IconData btnIcon = Icons.play_arrow;

AudioPlayer audioPlayer = new AudioPlayer();
bool isPlaying = false;
String currentSong = "";

Duration duration = new Duration();
Duration position = new Duration();

void playMusic(String url) async {
if(isPlaying && currentSong != url){
audioPlayer.pause();
int result = await audioPlayer.play(url);
if(result == 1){
setState(() {
currentSong = url;
});
}
} else if(!isPlaying){
int result = await audioPlayer.play(url);
if(result == 1){
setState(() {
isPlaying = true;
});
}
}
audioPlayer.onDurationChanged.listen((event) {
setState(() {
duration = event;
});
});

audioPlayer.onPositionChanged.listen((event) {
  setState(() {
    position = event;
  });
});

}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Text("My Playlist",
style: TextStyle(
color: Colors.black
),
),
elevation: 0,
),
body: Column(
children: [
//The app is composed of 2 parts
//The first one is the song playlist

      Expanded(
          child: ListView.builder(
              itemCount: musicList.length,
              itemBuilder: (context, index) =>
                customListTile(
                    onTap: (){
                      playMusic(musicList[index]['url']);
                      setState(() {
                        currentTitle = musicList[index]['title'];
                        currentCover = musicList[index]['coverUrl'];
                        currentSinger = musicList[index]['singer'];
                      });
                    },
                    title: musicList[index]['title'],
                    singer: musicList[index]['singer'],
                    cover: musicList[index]['coverUrl']
          )
          ),
      ),
      //The second one is the player
      Container(
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Color(0x55212121),
              blurRadius: 8
            )
          ]
      ),
        child: Column(
          children: [
            Slider.adaptive(
                value: position.inSeconds.toDouble(),
                min: 0,
                max: duration.inSeconds.toDouble(),
                onChanged: (value) {},
            ),
            Padding(
              padding: const EdgeInsets.only
                (left: 8, right: 8, bottom: 8),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Container(
                    height: 60,
                    width: 60,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(6),
                        image: DecorationImage(
                            image: NetworkImage(currentCover))
                    ),
                  ),
                  SizedBox(width: 10),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                            currentTitle,
                            style: TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.w600)
                        ),
                        SizedBox(height: 5),
                        Text(
                          currentSinger,
                          style: TextStyle(
                              color: Colors.grey,
                              fontSize: 14
                          ),
                        )
                      ],
                    ),
                  ),
                  IconButton(
                      onPressed: (){
                        if(isPlaying){
                          audioPlayer.pause();
                          setState(() {
                            btnIcon = Icons.pause;
                            isPlaying = false;
                          });
                        } else {
                          audioPlayer.resume();
                          setState(() {
                            btnIcon = Icons.play_arrow;
                            isPlaying = true;
                          });
                        }
                      },
                      iconSize: 42,
                      icon: Icon(btnIcon))
                ],
              ),
            )
          ],
        ),
      )
    ],
  ),
);

}
}

lib/main.dart:106:43: Error: The argument type 'String' can't be assigned to the parameter type 'Source'.

  • 'Source' is from 'package:audioplayers/src/source.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dev/audioplayers-4.1.0/lib/src/source.dart').
    int result = await audioPlayer.play(url);
    ^
    lib/main.dart:106:20: Error: This expression has type 'void' and can't be used.
    int result = await audioPlayer.play(url);
    ^
    please help me resolve this error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests