1

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.

pianoman102
  • 541
  • 8
  • 22

1 Answers1

0
  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.

PatrickMahomes
  • 844
  • 1
  • 6
  • 8
  • Well, naturally I can parse it manually, but that's what I'm trying to avoid. These packages are available for use, but I don't know anything about them. I'm hoping someone with protobuf experience has some insight here. Have you used protobufs before? – pianoman102 Jul 19 '21 at 23:06
  • @pianoman102 I'm sorry I don't quite understand your problem. What is your definition of "manual?" I only use protobuf when receiving data from one of my own apis, where I have control over the format. It saves me from having to use heavier data structures. You are receiving data as csv .txt files no matter what, so I can't understand why you want to use protobuf so badly. – PatrickMahomes Jul 20 '21 at 02:58
  • @pianoman102 you must have a function in your flutter app where you query the company's api and receive the files; can't you just decode them at that point using a function similar to what I provided? Sorry if I'm misunderstanding. – PatrickMahomes Jul 20 '21 at 03:01
  • manual parsing meaning I write my own parsing code. The packages I linked to are meant to do this. I can write my own parser, but I don't want to. One reason is because I want to learn how to use protobufs, and secondly, google's standard for this data utilizes protobufs and various packages to help decode it (https://developers.google.com/transit/gtfs-realtime/examples/code-samples specifically the other languages section) – pianoman102 Jul 20 '21 at 04:10
  • Ok, while I can't give you a whole tutorial on how to do it right here, maybe one reason your confused is that protobuf is just one piece of the puzzle. The packages you linked are mainly to compile .proto files, like [here](https://developers.google.com/protocol-buffers/docs/reference/dart-generated), which can't do anything on their own. For Dart, you'll want to use gRPC. Maybe [this](https://grpc.io/docs/languages/dart/basics/) will help you get started – PatrickMahomes Jul 20 '21 at 04:27