Skip to content

fetch always returns status 200 even when actual status should be different #175

Description

@nohackjustnoobb

When using the fetch API with flutter_js, the returned response object always reports a 200 status code, even if the actual HTTP status code is different. For example, when making a request to https://httpbin.org/status/400, the status is expected to be 400, but the reported resp.status is 200.

Sample Code to Reproduce:

import 'package:flutter/material.dart';
import 'package:flutter_js/extensions/fetch.dart';
import 'package:flutter_js/extensions/xhr.dart';
import 'package:flutter_js/flutter_js.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: FilledButton(
            onPressed: () async {
              final runtime = await getJavascriptRuntime().enableFetch();
              runtime.enableHandlePromises();
              runtime.enableXhr();

              JsEvalResult result = await runtime.evaluateAsync("""
              async function main() {
                console.log("Hello, World!!!");

                const resp = await fetch("https://httpbin.org/status/400");

                console.log("Response status:", resp.status);
              }

              main();
              """);
              runtime.executePendingJob();

              print(result);

              if (result.isPromise ||
                  result.stringResult == "Instance of 'Future<dynamic>'") {
                result = await runtime.handlePromise(result);
              }

              print(result);
            },
            child: Text('Click Me'),
          ),
        ),
      ),
    );
  }
}

Output:

I/flutter ( 2843): Hello, World!!!
I/flutter ( 2843): Instance of 'Future<dynamic>'
I/flutter ( 2843): Response status: 200
I/flutter ( 2843): null

Environment:

  • flutter_js version: 0.8.4
  • Platform: tested on Android and iOS
  • Flutter version: 3.32.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions