rw_tcx 0.1.0+3

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

rw_tcx #

name: rw_tcx

description: A simple package to read and write TCX file using Dart With this package it is possible to extract track points for a TCX file or write a valid TCX file from an array of track points

Getting Started #

It is a Dart only package, Flutter is not needed

How to install #

Check on to see how to install this package

How to use it #

There are only 2 APIs

  • readTCX
  • writeTCX

Example #

There is a complete example showing how to use readTCX and writeTCX For readTCX, the sample2.tcx stored in assets is coming from Garmin Connect export For writeTCX, the generated TCX file is sent to Google Drive in addition to be uploaded to Strava

To check your TCX file #

There is a nice online tool to get a detailed explanation of what is wrong in your TCX file

Tested on: #

So far only on Android 8.0 for the moment

Contributors welcome! #

If you spot a problem/bug or if you consider that the code could be better please post a new issue. I have developed this package to be able to upload a TCX to Strava so rTCX.dart is very basic. Feel free to contribute to add more features.

License: #

rw_tcx is provided under a MIT License. Copyright (c) 2019 Patrick FINKELSTEIN

[0.0.1] - 28/08/2019 #

  • Initial release

[0.0.2] - 29/08/2019 #

  • Added google drive export to example

[0.1.0] - 29/08/2019 #

  • Initial working release

[0.1.0+2] - 29/08/2019 #

  • Longer description in pubspec.yaml


import 'package:flutter/material.dart';

import 'package:flutter/services.dart' show rootBundle;

import 'secret.dart'; // Where Strava app secret and clientId stored

import 'package:rw_tcx/models/TCXModel.dart';
import 'package:rw_tcx/rTCX.dart';
import 'package:rw_tcx/wTCX.dart';

import 'package:path_provider/path_provider.dart';  // Needed for getApplications

import 'package:strava_flutter/strava.dart';
import 'package:strava_flutter/Models/fault.dart';

// Used by Google sign in 
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:googleapis/drive/v3.dart' as drive;
import 'dart:io' as io;

import 'package:http/http.dart'
    show BaseRequest, Response, StreamedResponse;
import 'package:http/io_client.dart';
class GoogleHttpClient extends IOClient {
  Map<String, String> _headers;

  GoogleHttpClient(this._headers) : super();

  Future<StreamedResponse> send(BaseRequest request) =>

  Future<Response> head(Object url, {Map<String, String> headers}) =>
      super.head(url, headers: headers..addAll(_headers));


main(List<String> args) async {
  print('Start of rw_tcx example');

  // First init of Strava API stuff
  // Do authentication with the right scope
  final strava = Strava(
      true, // To get display info in API

  bool isAuthOk = false;

  isAuthOk = await strava.oauth(clientId, 'activity:write', secret, 'auto');

  print('---> Strava Authentication result: $isAuthOk');

  // Start Google Auth to try access Google Drive

  final FirebaseAuth _auth = FirebaseAuth.instance;
  final GoogleSignIn googleSignIn = GoogleSignIn(
    scopes: [''],

  final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
  final GoogleSignInAuthentication googleSignInAuthentication =
      await googleSignInAccount.authentication;

   final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleSignInAuthentication.accessToken,
    idToken: googleSignInAuthentication.idToken,

  // final FirebaseUser user = await _auth.signInWithCredential(credential);
  AuthResult authResult = await _auth.signInWithCredential(credential);


  assert(await authResult.user.getIdToken() != null);

  final FirebaseUser currentUser = await _auth.currentUser();
  assert(authResult.user.uid == currentUser.uid);

  print('signInWithGoogle succeeded: ${authResult.user}');

  var client = GoogleHttpClient(await googleSignInAccount.authHeaders);

  var driveApi = drive.DriveApi(client);

  // Get the list of file from to root directory 
  // of the google drive
  // useful to check that driveApi is working 

  var fileList = await driveApi.files.list();
  // Display for 10 file names
  fileList.files.forEach((file) => print('---${})'));

  print('Read a TCX file');

  // Use the asset file to test without having to create internally a ride
  TCXModel rideData = TCXModel();

  String contents = '';

  try {
    // Read the test file sample1.tcx in assets

    contents = await rootBundle.loadString('assets/sample2.tcx');

    print('contents $contents');

    rideData = readTCX(contents);
  } catch (e) {
    print('error loading assets file:  $e');

  print('Write a TCX file');

  TCXModel tcxInfos = TCXModel();
  tcxInfos.points = rideData.points;
  tcxInfos.swVersionMajor = 'SWMajor';
  tcxInfos.swVersionMinor = 'SWMinor';
  tcxInfos.partNumber = 'Part Number';

  tcxInfos.dateActivity = DateTime(2019, 8, 15, 18, 28);

  await writeTCX(tcxInfos, 'generatedSample.tcx');

  print('End of write TCX test');

  final directory = await getApplicationDocumentsDirectory();
  String path = directory.path;

  var destFile = drive.File.fromJson({
    'mimeType': 'text/plain',
    'name': 'test_rw_tcx.txt', 

  io.File fileToUpload  = io.File('$path/generatedSample.tcx');
  drive.Media mediaContent = drive.Media(fileToUpload.openRead(), fileToUpload.lengthSync());

  var resultFile = await driveApi.files.create(destFile,
  uploadMedia: mediaContent);

  // Now use Strava API to upload the generatedTCX
  // Strava authentication has been done previously
  String dirUpload = (await getApplicationDocumentsDirectory()).path;

  Fault fault = await strava.uploadActivity(
      'Test rw_TCX', 'It is working!', '$dirUpload/generatedSample.tcx', 'tcx');

  print('This is the end!!');


Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  rw_tcx: ^0.1.0+3

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:rw_tcx/logTool.dart';
import 'package:rw_tcx/models/TCXModel.dart';
import 'package:rw_tcx/rTCX.dart';
import 'package:rw_tcx/wTCX.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
path_provider ^1.2.0 1.6.5
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
path_provider_macos 0.0.4
path_provider_platform_interface 1.0.1
platform 2.2.1
plugin_platform_interface 1.0.2
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies