0

I have made a helper function which is a task to fetch data from the server and return it as a promise-based response. Btw. I am a Flutter newbie :)

Helper function:

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

class FetchModules {
  void fetchAllModules() async {
    var url = Uri.parse('https://jsonplaceholder.typicode.com/todos/');
    var response = await http.get(url);
    if (response.statusCode == 200) {
      String data = response.body;
      var decodedData = jsonDecode(data);
      return decodedData;
    }
  }
}

Widget where I want to get a response:

void initState() {
    super.initState();
    FetchModules().fetchAllModules().then((){});
}

VSCode Error:

This expression has a type of 'void' so its value can't be used.
Try checking to see if you're using the correct API; 
there might be a function or call that returns void you didn't expect.
Also check type parameters and variables which might also be void.dart(use_of_void_result)

So, how to fix this?

Thanks!

Milos N.
  • 4,416
  • 6
  • 18
  • 31
  • 1
    `fetchAllModules` returns `void`, so you can't do anything with its return value. If you want to allow callers to be notified when `fetchAllModules` completes, it must return a `Future` instead (specifically a `Future` in this case). You also might want to see [What is a Future and how do I use it?](https://stackoverflow.com/q/63017280/) – jamesdlin Aug 04 '21 at 22:46
  • @jamesdlin, thank you! Omg :) – Milos N. Aug 04 '21 at 22:52

0 Answers0