'This function has a return type of 'FutureOr<Xmodel>, but doesnt end with a return statement
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}");
}
}
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ankit Kumar Maurya |
