More
How to Use Uint8List in Flutter and Persist It in the Backend Using Back4app
12 min
introduction in digital systems, files are often represented as a sequence of bytes, and dart provides an efficient way to handle byte data using uint8list a uint8list is a fixed length list of unsigned 8 bit integers, meaning each number ranges from 0 to 255 this structure is useful for working with binary data, such as images or files, where memory efficiency is crucial in this tutorial, we'll cover how to work with uint8list in flutter, convert files into byte lists, and persist the data in back4app's backend using the parse sdk for flutter prerequisites before starting, ensure you have the following a back4app account sign up for a free account at back4app com https //www back4app com a flutter development environment set up on your local machine follow the flutter installation guide https //flutter dev/docs/get started/install if you haven't set it up yet basic knowledge of dart, flutter widgets, and using back4app for backend services step 1 – setting up your back4app backend create a back4app project log in to your back4app account https //www back4app com/ and create a new project create parse classes for this tutorial, create a parse class named filestorage to store the binary data filename (string) the name of the file filedata (file) the binary data of the file get your back4app credentials navigate to your project settings to retrieve your application id and client key, which you’ll need to connect your flutter app to back4app step 2 – setting up the flutter project create a new flutter project open your terminal or command prompt and run flutter create uint8list example add dependencies open pubspec yaml and add the following dependencies dependencies flutter sdk flutter parse server sdk flutter latest version file picker latest version path provider latest version run flutter pub get to install these dependencies initialize parse in your app in lib/main dart , initialize the parse sdk import 'package\ flutter/material dart'; import 'package\ parse server sdk flutter/parse server sdk flutter dart'; void main() async { widgetsflutterbinding ensureinitialized(); const keyapplicationid = 'your back4app app id'; const keyclientkey = 'your back4app client key'; const keyparseserverurl = 'https //parseapi back4app com'; await parse() initialize(keyapplicationid, keyparseserverurl, clientkey keyclientkey, autosendsessionid true); runapp(myapp()); } class myapp extends statelesswidget { @override widget build(buildcontext context) { return materialapp( home uint8listexample(), ); } } replace 'your back4app app id' and 'your back4app client key' with your actual back4app credentials step 3 – working with uint8list in flutter file selection and reading use the file picker package to select a file from the device and convert it into a uint8list import 'dart\ io'; import 'dart\ typed data'; import 'package\ flutter/material dart'; import 'package\ file picker/file picker dart'; import 'package\ path provider/path provider dart'; class uint8listexample extends statefulwidget { @override uint8listexamplestate createstate() => uint8listexamplestate(); } class uint8listexamplestate extends state\<uint8listexample> { uint8list? filebytes; string? filename; future\<void> pickfile() async { filepickerresult? result = await filepicker platform pickfiles(); if (result != null) { platformfile file = result files first; setstate(() { filebytes = file bytes; filename = file name; }); } } @override widget build(buildcontext context) { return scaffold( appbar appbar(title text('uint8list example')), body center( child column( mainaxisalignment mainaxisalignment center, children \[ elevatedbutton( onpressed pickfile, child text('pick a file'), ), if ( filename != null) text('selected file $ filename'), if ( filebytes != null) elevatedbutton( onpressed savetobackend, child text('save to backend'), ), ], ), ), ); } future\<void> savetobackend() async { // implement the saving logic in the next step } } this code lets the user select a file, reads the file as a uint8list , and displays the file name and a button to save it step 4 – persisting the uint8list in back4app to store the file in back4app, we use the parsefile object, which allows us to upload binary data like images or documents saving the file to back4app update the savetobackend method to persist the selected file in back4app future\<void> savetobackend() async { if ( filebytes != null && filename != null) { final parsefile = parsefile(null, name filename, bytedata filebytes); // create a new parseobject to store the file final filestorage = parseobject('filestorage') set('filename', filename) set('filedata', parsefile); // save the file in back4app final response = await filestorage save(); if (response success) { scaffoldmessenger of(context) showsnackbar(snackbar( content text('file saved successfully!'), )); } else { scaffoldmessenger of(context) showsnackbar(snackbar( content text('failed to save file '), )); } } } this method uploads the file as binary data to back4app using a parsefile if successful, it displays a confirmation message step 5 – running the app run your app using flutter run you should see a button to pick a file, followed by an option to save it to the backend once selected verify the data in back4app by logging into your back4app dashboard and checking the filestorage class you should see the file saved along with its name best practices for storing binary data when working with uint8list and binary data, here are some best practices use back4app's parsefile is an efficient way to store and retrieve binary data such as images, videos, and documents avoid storing large files directly in parse objects if your files are large, consider using a storage service such as aws s3 and store the file url in your parse object instead of the file itself compression and optimization for large images or files, consider compressing the file before uploading it to improve performance conclusion in this tutorial, we covered how to work with uint8list in flutter, convert files into byte lists, and persist this data in back4app using the parse sdk uint8list provides a memory efficient way to handle byte data in dart, making it suitable for file operations like image uploads, document storage, and more integrating this with back4app allows you to easily store and manage binary data in the cloud for more information, visit the flutter documentation https //flutter dev/docs and back4app documentation https //www back4app com/docs happy coding!