[Custom Action] Download Firestore Document As JSON

Hello, Here we go again, engage your seat belt, i hope you will enjoy the ride. Level : Intermediate We will rely on the new OpenAI Integration for time saving. First Step : Create a temporary Custom Function, called it Download Firestore Document As JSON ( or whatever, we will delete it in a minute ) Set this custom Function as this :[image.png]Go to the Code section, then enable the  OpenAI "Helper" : Type : export a firestore document to a JSON Try until the code is generated, sometime its fail. You should end up having this kind of result : Copy Code to function and/or save it to notepad 🤷‍♂️ return { code that we need to save }[image.png]The IA help you to construct your Json kind of format, avoiding you the long step to manually construct it !! Imagine having a document with hundred of fields. ( i'm sure there is a proper way of creating a loop to generate it from the document query itself ) This step has to be repeat on every Collection you want to extract a Document From.

Now create a Custom Action and set it up like this :[image.png]Add dependencies :

 path_provider
download
add import in the code :
import 'package:path_provider/path_provider.dart';
import 'package:download/download.dart';
import 'dart:math' as math;
import 'dart:convert';
import 'dart:io';

The Code bellow :
Copy inbeetween the Final data = { your document constructor form the above AI function }
import 'package:path_provider/path_provider.dart';
import 'package:download/download.dart';
import 'dart:math' as math;
import 'dart:convert';
import 'dart:io';

Future downloadDocumentAsJSON(CommandesRecord document) async {
 

  final data = {
Copy Here the document constructor from the above AI function         
};
  
final filename = Put here how you name the file, it could be a field ID value + '.json';
final stream = Stream.fromIterable(data.toString().codeUnits);

  return download(stream, filename);
}You will need to make some changes on every field to make to a proper JSON Format. ( again i'm sure there is a proper way )

Exemple to apply on every field :
Before :

'commandeActivite': document.commandeActivite,
After :

'"commandeActivite"': json.encode(document.commandeActivite.toString()),  

Finnish, Add this Action to a download button and pass as argument your Document Query [image.png] Live Demo :[DocumentExport_.gif] Next :  1. Handling utf 8 characters  2. implementing .csv or/and Excel file format.  3. Collection Export ( pending )

2
9 replies