"Uncaught error: " in Flutter Firebase deployment

Viewed 45

Any suggested remedies would be helpful.

I have a flutter web app project. It runs locally without console errors: flutter run.

It accesses an API through a http.get so I had to --disable-web-security to stop receiving a CORS error during development.

The API is served over HTTPS.

When I deploy it using,

flutter clean
flutter web build
firebase deploy

It deploys, and all looks correct when opened.

However, when I click the submit button to send to the API, I get an Uncaught Error ...

How can I find the error? the main.dart.js file is unreadable.

Update: flutter build web --profile --dart-define=Dart2jsOptimization=O0 allows for a slightly more useful error log

Error in console:

main.dart.js:8261 Uncaught Error
    at Object.StackTrace_current (main.dart.js:12588:40)
    at BrowserClient_send_closure0.call$1 (main.dart.js:131527:88)
    at _RootZone.runUnary$2$2 (main.dart.js:60361:18)
    at _RootZone.runUnary$2 (main.dart.js:60365:19)
    at _Future__propagateToListeners_handleValueCallback.call$0 (main.dart.js:59815:51)
    at Object._Future__propagateToListeners (main.dart.js:10985:93)
    at _Future._complete$1 (main.dart.js:59667:11)
    at Object._cancelAndValue (main.dart.js:11147:16)
    at Stream_first_closure0.call$1 (main.dart.js:59896:9)
    at _EventStreamSubscription_onData_closure.call$1 (main.dart.js:66619:30)

There are no flutter doctor errors

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 3.1.0-0.0.pre.2167, on Ubuntu 20.04.4 LTS 5.15.0-43-generic, locale en_IE.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2021.2)
[✓] Android Studio
[✓] VS Code
[✓] Connected device (2 available)
[✓] HTTP Host Availability

The request

class xApi {

  _setHeaders() =>
      {'Accept': 'application/json', 'Access-Control-Allow-Origin': '*'};

  Future<List<String>> returnList(String agr1, arg2) async {
    var fullUrl = "https://point.to.url/api?i=$arg1&o=$arg2";
    var response = await http.get(Uri.parse(fullUrl), headers: _setHeaders());
    var body = json.decode(response.body);
    var result = json.decode(body['result1']);

    return List<String>.from(result);
  }
}

The button's onsubmit

onSubmitted: (String value) async {
    var result1 = await xApi().returnList(input1, input2);
    if (result1.isNotEmpty) {
      setState(() => {listOfAnswers = result1});
    }
}

The API's route:

@app.route("/api")
def returnListOfMnemonics():
    print("request.args = ", request.args)
    arg1 = request.args.get("i")
    arg2 = True if (request.args.get("o") == "1") else False
    decoder = Decoder(arg2)
    resp = make_response(
        jsonify({"results": json.dumps(decoder.decode(arg1.lower()))})
        )
    # TODO this should be set to the actual origins
    resp.headers['Access-Control-Allow-Origin'] = '*'
    resp.headers["Content-Type"] = "application/json"

    return resp

1 Answers

After spending time on this, finding graphs like this made it appear like a simple issue to fix. CORS

I tried configuring the client and server with the headers mentioned in the graph, but the solution, in the end, was to install flask-cors in the API with no specific configuration aws_cors

I don't understand what I was doing wrong in configuring the headers.

Related