londonappbrewery / Clima-Flutter

Starter code for the Clima Project from the Complete Flutter Development Bootcamp
https://www.appbrewery.co
153 stars 740 forks source link

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

Open RIP321 opened 5 years ago

RIP321 commented 5 years ago
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 commented 5 years ago

Im having issues with same problem...

RIP321 commented 5 years ago

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 commented 4 years ago

In NetworkHelper as you are returning something it cant be void

tamersolieman commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Im having issues with same problem...

me too

AslanDevbrat commented 3 years ago

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 commented 3 years ago

i have also same problem.

malikdanishalii commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago
Screenshot 2022-11-16 at 7 57 56 PM
maheenqayyum commented 1 year ago

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'.