Flutter RSS Feed XML 请求问题

问题描述

enter image description here

谁能告诉我出了什么问题?据我所知,这段代码是正确的,但我不明白我在哪里在 bash 中收到此 XMLHttpRequest 错误

我最初遵循这个 - https://www.youtube.com/watch?v=F0xh7LMr_V0,我还发现其他教程解释了同样的事情,但没有人真正提到这个错误

任何帮助将不胜感激,

最诚挚的问候,

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,);
  }

  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(),);
  }
}

解决方法

这是在网上吗?你没有提供太多帮助 - 你得到的错误和堆栈跟踪......

您可能在使用这个“CachedNetworkImage”时遇到了 CORS 错误

,

来自 Google Flutter Group 的 Flutter Group 建议,代码在同一台服务器上运行,但认为问题与 CORS 相关,现在将对此进行进一步调查:) 再次感谢!