Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for Web Support: CORS Issue #9

Open
Turskyi opened this issue Sep 28, 2024 · 1 comment
Open

Request for Web Support: CORS Issue #9

Turskyi opened this issue Sep 28, 2024 · 1 comment

Comments

@Turskyi
Copy link

Turskyi commented Sep 28, 2024

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
  1. 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:

  • Flutter version: 3.24.3
  • Dart version: 3.5.3
  • Platform: Web

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants