Flutter protobufs working with static and/or realtime GTFS data

Viewed 262

I'm trying to build a flutter app that is consuming transit data. Some of this data is coming in as GTFS data and I'm having a really hard time finding information as to how to work with it in dart/flutter. I know about the dart bindings for protobufs, but I don't see how that can help me with parsing gtfs data in flutter. I have three questions:

  1. The static gtfs data is coming in as txt/csv files and contains various information about the transit system. I can manually parse this with python, and export to json and import it as an asset to the flutter project. I don't particularly like this because anytime the data changes, I would have to download the zip, unzip, parse, export to json, import to flutter. Clunky. How can I do this primarily in dart? (related, there is a protobuf package for dart, but I don't get what I'm getting with the package).

  2. Does realtime gtfs data typically come in the txt/csv format, or is it xml/json? I'm wondering because the company I'm contracting with will have a gtfs feed in the future, but my contact at the company doesn't know in what format the api will pass on the data.

  3. If it is txt/csv format, am I able to parse it realtime with dart and the packages/tools I've linked to above? If it's json/xml data, I don't have a problem.

1 Answers
  1. I'm not sure what you want to do with the data, but this answer will help you convert the .txt files into strings. After this point you can do whatever you want with them, for instance:
List<String> routes = File('routes.txt').readAsLinesSync();
List<String> fieldHeaders = routes[0].split(",");
print(fieldHeaders);
for (String route in routes.sublist(1, routes.length)) {
  List<String> routeInfo = route.split(",");
  print(routeInfo);
}
  1. According to the link you provided, part of the definition of GTFS is "All files must be saved as comma-delimited text," so you can be pretty sure that's how you're going to get the data.

  2. Yes, Dart can easily handle csv .txt files. The reason not much is written about it is that most people use the native functionality to do it themselves.

Related