Search code examples
listfluttermethodsfuturedefined

The return type 'List<Menu>' isn't a 'Future<Menu>', as defined by the method 'anaMenuListeyiAl'


https://paste.ubuntu.com/p/HzMF3ShspF/ i created this model class to get information from json data when page is loaded

https://paste.ubuntu.com/p/hKmNTjFwkX/ this is my function to reach my data by using menuFromJson method but now it says

The return type 'List' isn't a 'Future', as defined by the method 'anaMenuListeyiAl'. What should i do i don't know, is there someone who can help me.


Solution

  • Two things wrong here: First, your method Future anaMenuListeyiAl() is a future that try to return a single menu an you want to make that return a list instead so change :

    Future<Menu> anaMenuListeyiAl()async {
    

    to

    Future<List<Menu>> anaMenuListeyiAl()
    

    Then, as i said, your method return a future so you have to await for it or use it in a futurebuilder.

    List<Menu> menuList = await anaMenuListeyiAl();
    

    or if in a initstate:

    anaMenuListeyiAl().then((menuList){
     print(menuList);
    });
    

    UPDATE CODE:

    This is the right function:

      Future<List<Menu>> anaMenuListeyiAl() async{
    List<Menu> menuList;
    final response = await http.post(
        global.apiUrl + global.anaMenuListe,
        headers: {"Content-type": "application/json"},
        body: json.decode({
          "key": "${global.key}", 
          "islem_kodu": "00X"
        }));
    if(response.statusCode==200){
     menuList = menuFromJson(response.body);
    }else{
     print("Unathorized");
    }
    
    return menuList;
    }