flyerhq / flutter_link_previewer

Customizable link and URL preview extracted from the provided text with the ability to render from the cache. Ideal for chat applications.
MIT License
72 stars 76 forks source link

PreviewData is Empty and no preview #61

Open nc-jeni opened 5 months ago

nc-jeni commented 5 months ago

The following code is placed on my StatefulWidget.

    LinkPreview(
      enableAnimation: true,
      onPreviewDataFetched: (data) {
        print(data);  // data is empty
        setState(() {
          httpdata = data;
        });
      },
      previewData: httpdata,
      text: s, // the url is passed here
      width: MediaQuery.of(context).size.width * 0.7,
    ),

Called with two URLs on the Web platform I get the following response in the data parameter and (of course) no preview is shown, but why is the PreviewData object empty? I have a feeling that the data are never fetched because of an async call somewhere in the flutter_link_previewer library?

_PreviewData(null, null, https://dr.dk, null)
_PreviewData(null, null, https://tv2.dk, null)

An error comes in getPreviewData when http.get is called and it goes to the catch block.

[log] XMLHttpRequest error.

Please have a look at this post for solving the problem: dart-flutter-http-request-raises-xmlhttprequest-error as it is probably the issue???

nc-jeni commented 5 months ago

I figured it out. Running from a mobile platform it works, but from web it does not. The reason was because of cross cors.

headers: {
  "Access-Control-Allow-Origin": "*",
  "Content-Type": "application/json",
  "Accept": "*/*"
}

Adding the option using such request headers might be an idea? @demchenkoalex