ivofernandes / yahoo_finance_data_reader

Apache License 2.0
7 stars 3 forks source link

Request for Web Support: CORS Issue #9

Open Turskyi opened 1 month ago

Turskyi commented 1 month ago

Hello,

I am using the yahoo_finance_data_reader package (version 1.0.11) in my Flutter project. The package works perfectly on Android, but I encounter a CORS issue when trying to run the app on the web.

Steps to Reproduce:

  1. Add the yahoo_finance_data_reader package to pubspec.yaml:

    dependencies:
    flutter:
    sdk: flutter
    yahoo_finance_data_reader: ^1.0.11
  2. Use the following code to fetch stock data for “GOOG”:

import 'package:flutter/material.dart';
import 'package:yahoo_finance_data_reader/yahoo_finance_data_reader.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'yahoo_finance_data_reader 1.0.11 Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final String _ticker = 'GOOG';
  YahooFinanceResponse? _response;
  bool _isLoading = true;

  @override
  void initState() {
    super.initState();
    _fetchStockData();
  }

  Future<void> _fetchStockData() async {
    try {
      final DateTime now = DateTime.now();
      YahooFinanceResponse response =
          await YahooFinanceDailyReader().getDailyDTOs(
        _ticker,
        startDate: DateTime(now.year, now.month, now.day),
      );
      setState(() {
        _response = response;
        _isLoading = false;
      });
    } catch (e) {
      setState(() {
        _isLoading = false;
      });

      debugPrint('Failed to fetch stock data: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text('yahoo_finance_data_reader 1.0.11 demo'),
      ),
      body: Center(
        child: _isLoading
            ? CircularProgressIndicator()
            : _response == null || _response!.candlesData.isEmpty
                ? Text('Failed to load stock data')
                : Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        'Stock Data for $_ticker:',
                        style: Theme.of(context).textTheme.headlineSmall,
                      ),
                      SizedBox(height: 20),
                      Text(
                        'Open: ${_response!.candlesData.first.open}',
                        style: Theme.of(context).textTheme.bodyLarge,
                      ),
                      Text(
                        'Close: ${_response!.candlesData.first.close}',
                        style: Theme.of(context).textTheme.bodyLarge,
                      ),
                      Text(
                        'High: ${_response!.candlesData.first.high}',
                        style: Theme.of(context).textTheme.bodyLarge,
                      ),
                      Text(
                        'Low: ${_response!.candlesData.first.low}',
                        style: Theme.of(context).textTheme.bodyLarge,
                      ),
                    ],
                  ),
      ),
    );
  }
}

Expected Behavior: The app should fetch and display stock data for “GOOG” on both Android and web platforms.

Actual Behavior: The app works on Android, but on the web, it fails with the following error:

Failed to fetch stock data: DioException [connection error]: The connection errored: The XMLHttpRequest onError callback was called. This typically indicates an error on the network layer. This indicates an error which most likely cannot be solved by the library.

Additional Information:

Logs/Output:

Failed to fetch stock data: DioException [connection error]: The connection errored: The XMLHttpRequest onError callback was called. This typically indicates an error on the network layer. This indicates an error which most likely cannot be solved by the library.

Request: Could you please add web support to the yahoo_finance_data_reader package or provide guidance on how to handle CORS issues when using this package on the web?

Thank you for your assistance!

ivofernandes commented 1 month ago

The CORS problem is on yahoo side I guess

here you can check the request code: https://github.com/ivofernandes/yahoo_finance_data_reader/blob/master/lib/src/daily/services/yahoo_finance_daily_reader.dart#L67