0

enter image description hereCan anyone please advise what is going wrong? As far as I can this code is correct but I can't understand where i'm getting this XMLHttpRequest error in bash.

I originally followed this - https://www.youtube.com/watch?v=F0xh7LMr_V0 and I have also found other tutorials explaining the same thing but no one really mentioning this error.

Any help would be greatly appreciated,

Kindest Regards,

B

import 'package:ad/main.dart';
import 'package:flutter/material.dart';

import 'dart:async';
import 'package:webfeed/webfeed.dart';
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
import 'package:cached_network_image/cached_network_image.dart';

class NewsFeed extends StatefulWidget {
  NewsFeed() : super();

  final String title = 'RSS Feed';

  @override
  NewsFeedState createState() => NewsFeedState();
}

class NewsFeedState extends State<NewsFeed> {
  static const String FEED_URL =
      'https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss';
  RssFeed _feed;
  String _title;
  static const String loadingFeedMsg = 'Loading Feed...';
  static const String feedLoadErrorMsg = 'Error Loading Feed...';
  static const String feedOpenErrorMsg = 'Error Opening Feed...';
  static const String placeholderImg = 'images/no_image.png';
  GlobalKey<RefreshIndicatorState> _refreshKey;

  updateTitle(title) {
    setState(() {
      _title = title;
    });
  }

  updateFeed(feed) {
    setState(() {
      _feed = feed;
    });
  }

  Future<void> openFeed(String url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
        forceSafariVC: true,
        forceWebView: false,
      );
      return;
    }
    updateTitle(feedOpenErrorMsg);
  }

  load() async {
    updateTitle(loadingFeedMsg);
    loadFeed().then((result) {
      if (null == result || result.toString().isEmpty) {
        updateTitle(feedLoadErrorMsg);
        return;
      }
      updateFeed(result);
      updateTitle(_feed.title);
    });
  }

  Future<RssFeed> loadFeed() async {
    try {
      final client = http.Client();
      final response = await client.get(FEED_URL);
      return RssFeed.parse(response.body);
    } catch (e) {
      print(e);
      // handle any exceptions here
    }
    return null;
  }

  @override
  void initState() {
    super.initState();
    _refreshKey = GlobalKey<RefreshIndicatorState>();
    updateTitle(widget.title);
    load();
  }

  title(title) {
    return Text(
      title,
      style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.w500),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  subtitle(subTitle) {
    return Text(
      subTitle,
      style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w100),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
  }

  thumbnail(imageUrl) {
    return Padding(
      padding: EdgeInsets.only(left: 15.0),
      child: CachedNetworkImage(
        placeholder: (context, url) => Image.asset(placeholderImg),
        imageUrl: imageUrl,
        height: 50,
        width: 70,
        alignment: Alignment.center,
        fit: BoxFit.fill,
      ),
    );
  }

  rightIcon() {
    return Icon(
      Icons.keyboard_arrow_right,
      color: Colors.grey,
      size: 30.0,
    );
  }

  list() {
    return ListView.builder(
      itemCount: _feed.items.length,
      itemBuilder: (BuildContext context, int index) {
        final item = _feed.items[index];
        //return ListTile(
        return ListTile(
          title: title(item.title),
          //subtitle: subtitle(item.description),
          subtitle: subtitle(item.description),

          leading: item.enclosure?.url != null
              ? thumbnail(item.enclosure.url)
              : SizedBox.shrink(), // Replace Container() with SizedBox.shrink()
          trailing: rightIcon(),
          contentPadding: EdgeInsets.all(5.0),
          onTap: () => openFeed(item.link),
        );
      },
    );
  }

  isFeedEmpty() {
    return null == _feed || null == _feed.items;
  }

  body() {
    return isFeedEmpty()
        ? Center(
            child: CircularProgressIndicator(),
          )
        : RefreshIndicator(
            key: _refreshKey,
            child: list(),
            onRefresh: () => load(),
          );
  }

// /////////////////////////////////////////////////
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: DrawerCodeOnly(),
      appBar: AppBar(
        title: Text(_title),
      ),
      body: body(),
    );
  }
}

2 Answers2

0

Is this on web? You don’t give much to help - error you get and the stack trace...

You’re probably getting a CORS error using this ‘CachedNetworkImage'

Agreensh
  • 1,305
  • 1
  • 12
  • 15
  • It did originally show an issue with the CachedNetworkImage, but that error just stopped loading and showing, since then it just showes this error. – Bridget Sarah Feb 18 '21 at 15:25
  • Flutter Group from Google Flutter Group has advised, code works on same server but believes issue is related to CORS will investigate further into this now :) Thanks again! – Bridget Sarah Feb 18 '21 at 17:05
0

Flutter Group from Google Flutter Group has advised, code works on same server but believes issue is related to CORS will investigate further into this now :) Thanks again!