Dart parsing RSS

Kristian Tuusjarvi
2 min readJan 5, 2024

In this article, I will explain how to parse RSS feeds to the Dart application.

What is RSS?

RSS is an XML-formatting often used to fetch information from websites. For example, Medium has its own RSS feed. We can use Medium’s RSS feed to fetch data on our profiles, topics, and publications and then embed those into our websites and applications. We are using the rss_dart package to parse the RSS.

RSS feed of Medium.

RSS Example

To test the RSS feed, navigate to this address: https://medium.com/feed/@, and add your profile name after the ‘@’ sign. For me, this is https://medium.com/feed/@ktuusj.

Example RSS feed, XML.

Example code

Add packages for https calls and the rss_dart for parsing.

#pubspec.yaml
dependencies:
rss_dart: ^1.0.4
http: ^1.1.0

Dart code.

import 'package:http/http.dart';
import 'package:rss_dart/dart_rss.dart';

void main(List<String> arguments) {
Client client = Client();
getRssFeed(client).then((value) {
List<Information> informationList = parseRSS(value);
printInformationList(informationList);
});
}

void printInformationList(List<Information> informationList) {
print('--- Information List ---');
for (var information in informationList) {
print('Date: ${information.date}');
print('Title: ${information.title}');
print('Text: ${information.text}');
print('Image URL: ${information.imageUrl ?? 'N/A'}');
print('Link: ${information.link ?? 'N/A'}');
print('------------------------');
}
}

Future<RssFeed> getRssFeed(Client client) async {
Response response = Response('', 200);
try {
response = await client.get(Uri(
scheme: 'https',
host: 'medium.com',
path: '/feed/@ktuusj',
));
} catch (e) {
print(e);
}

return RssFeed.parse(response.body);
}

List<Information> parseRSS(RssFeed feed) {
List<Information> informationList = [];
for (var item in feed.items) {
Information blogInformation = Information(
date: item.pubDate!,
title: item.title!,
text: item.content!.value,
imageUrl: item.content?.images.first,
link: feed.items.first.link,
);
informationList.add(blogInformation);
}
return informationList;
}

class Information {
String date;
String title;
String text;
String? link;
String? imageUrl;
String? uuid;
Information({
required this.date,
required this.title,
required this.text,
this.link,
this.imageUrl,
this.uuid,
});
}

Leave a comment or clap if you enjoyed it or want to give feedback. :)

Link to code.

--

--