ipfs_http_rpc and flutter_ipfs not working properly

  Kiến thức lập trình

Im a civil engineering student currently doing my fyp which is developing a Dapp for dispute resolution, my skill level is Im good in flutter but I only know the concept of IPFS now the problem:

I want to upload files on ipfs (Inter-Planetary File System) through my app which Im making on flutter. For this I used the ipfs_http_rpc package and flutter_ipfs package from Dart packages both with the same results which is that both these packages in their upload functions only accept string data. The codes for both these classes are

for ipfs_http_rpc:

Map<String, dynamic> response = await ipfs.add(path: fileBytes, wrapWithDirectory: true);

for flutter_ipfs:

await FlutterIpfs().uploadToIpfs(BytesList.toString());

With the first method you can see I tried to upload file path to ipfs but I got the error

Error at PDF picker: On web pathis alwaysnull, You should access bytes property instead, Read more about it [here](https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ)
Which I thought meant that I should upload Bytes data to IPFS, so in the next snippet I converted it into bytes like this (see line 7)

 static Future<String?> pickPdf(BuildContext context) async {
    try {
      FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['pdf'],
      );

      if (result != null) {
        List<int> BytesList = result.files.first.bytes!;
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (BuildContext context) => ProgressDialog(status: 'Uploading to IPFS'),
        );

        cid = await FlutterIpfs().uploadToIpfs(BytesList.toString());
        debugPrint(cid);

        Navigator.pop(context);
        return "shit";

I ultimately converted this into String because that’s how the “upload to ipfs” function accepts arguments, then I get this error
Error at IPFS Service - uploadImage: Unsupported operation: _Namespace Error at PDF picker: Unsupported operation: _Namespace
I asked chatGPT about this and it said that IPFS needs bytes data and it cannot work with Strings as it cannot read them. So as a last ditch effort I converted the List into a List and tried to feed that into the function like this

static Future<String?> pickPdf(BuildContext context) async {
    try {
      FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['pdf'],
      );

      if (result != null) {
        List<int> BytesList = result.files.first.bytes!;
        List<String> BytesString = BytesList.map((e) => e.toString()).toList();
        print(BytesList);
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (BuildContext context) => ProgressDialog(status: 'Uploading to IPFS'),
        );

        cid = await FlutterIpfs().uploadToIpfs(BytesString);
        debugPrint(cid);

        Navigator.pop(context);
        return "shit";
      }

with which I get the error that a value of type List cannot be assigned to argument for String.

Now I can’t come to any other explanation other than this that the library is faulty. But it has many good reviews and all other packages for ipfs support have requirement for a String argument in the functions, so I cannot understand how I can implement ipfs support on my flutter app when the function’s for uploading do not accept List or List and ipfs does not accept String

Can please anyone who’s good with flutter and ipfs help me solve this issue and tell me how I can employ a function that uploads files on ipfs through my application, thank you!

Here’s my entire code for the file picker library if it helps, Some part of the code is commented out.

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter_ipfs/flutter_ipfs.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:ipfs_http_rpc/ipfs.dart';



import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter_ipfs/flutter_ipfs.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:ipfs_http_rpc/ipfs.dart';

/*class PdfPickerService {
  static String cid = ""; // This will store the CID of the uploaded file
  static Future<String?> pickPdf(BuildContext context) async {
    try {
      final result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['pdf'],
      );

      if (result != null) {
        String? fileBytes = result.files.first.path!;
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (BuildContext context) => ProgressDialog(status: 'Uploading to IPFS'),
        );
        Ipfs ipfs = Ipfs();
        Map<String, dynamic> response = await ipfs.add(path: fileBytes, wrapWithDirectory: true);
        cid = await response.hashCode as String;
        debugPrint(cid);

        Navigator.pop(context);
        return 'fuck flutter';
      } else {
        Fluttertoast.showToast(msg: 'No PDF Selected');
        return null;
      }
    } catch (e) {
      debugPrint('Error at PDF picker: $e');
      SnackBar(
        content: Text(
          'Error at PDF picker: $e',
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 15),
        ),
      );
      return null;
    }
  }
}*/



class PdfPickerService {
  static String cid = ""; // This will store the CID of the uploaded file

  static Future<String?> pickPdf(BuildContext context) async {
    try {
      FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowedExtensions: ['pdf'],
      );

      if (result != null) {
        List<int> BytesList = result.files.first.bytes!;
        List<String> BytesString = BytesList.map((e) => e.toString()).toList();
        print(BytesList);
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (BuildContext context) => ProgressDialog(status: 'Uploading to IPFS'),
        );

        cid = await FlutterIpfs().uploadToIpfs(BytesString);
        debugPrint(cid);

        Navigator.pop(context);
        return "shit";
      } else {
        Fluttertoast.showToast(msg: 'No PDF Selected');
        return null;
      }
    } catch (e) {
      debugPrint('Error at PDF picker: $e');
      SnackBar(
        content: Text(
          'Error at PDF picker: $e',
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 15),
        ),
      );
      return null;
    }
  }
}

LEAVE A COMMENT