This function has a return type of 'FutureOr<Xmodel>, but doesnt end with a return statement

202 Views Asked by At

I'm trying to integrate a stock API into my app. I'm trying to create a service page but I get this error. I searched for similar topics but I couldn't find a solution.

import 'dart:convert';
import 'dart:io';
import 'package:myapp/models/apis/stocks.dart';
import 'package:http/http.dart' as http;

class StocksService {
  final String url = "https://api.collectapi.com/economy/hisseSenedi";

  Future<StocksModel> fetchStocks() async {
    var res = await http.get(Uri.parse(url),
      headers: {
        HttpHeaders.authorizationHeader:
            "apikey xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      },
    );
    if (res.statusCode == 200) {
      var jsonBody = StocksModel.fromJson(jsonDecode(res.body));
      return jsonBody;
    } else {
      print("İstek başarısız oldu => ${res.statusCode}");
    }
  }
}
1

There are 1 best solutions below

0
Ankit Kumar Maurya On

You are not returning anything in your else block. It this is what you require in your code then change the return type of the function to dynamic. And if not then you are required to return the same data type in the else block too.