flutter_link_previewer icon indicating copy to clipboard operation
flutter_link_previewer copied to clipboard

PreviewData is Empty and no preview

Open nc-jeni opened this issue 1 year ago • 1 comments

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 avatar Apr 24 '24 11:04 nc-jeni

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

nc-jeni avatar Apr 25 '24 13:04 nc-jeni