How to pass variable from flutter to native objective c

844

Change:

NSString *filePath = call.arguments(@"filePath");

to

NSString *filePath = call.arguments[@"filePath"];
Share:
844
bjmcallister
Author by

bjmcallister

Updated on December 12, 2022

Comments

  • bjmcallister
    bjmcallister over 1 year

    I have found plenty of information on passing data from flutter to android using method channel but none for objective c. I understand calling functions, but not passing data. Below is an example of what I have.

      Future<void> _uploadImage(String filePath) async {
        try {
          await platform.invokeMethod('uploadImage', {"filePath": filePath});
        } on PlatformException catch (e) {
          print("Failed to get token: '${e.message}'.");
        }
      }
    
    else if([@"uploadImage" isEqualToString:call.method]){
          DBUserClient *client = [DBClientsManager authorizedClient];
          NSString *filePath = call.arguments(@"filePath");
          NSLog(filePath, result);
          NSData *fileData = [filePath dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
    
        [[[client.filesRoutes uploadData:filePath inputData:fileData]
          setResponseBlock:^(DBFILESFileMetadata *result, DBFILESUploadError *routeError, DBRequestError *networkError) {
              if (result)
              {
                  NSLog(@"%@\n", result);
              } else
              {
                  NSLog(@"%@\n%@\n", routeError, networkError);
              }
          }] setProgressBlock:^(int64_t bytesUploaded, int64_t totalBytesUploaded, int64_t totalBytesExpectedToUploaded) {
              NSLog(@"\n%lld\n%lld\n%lld\n", bytesUploaded, totalBytesUploaded, totalBytesExpectedToUploaded);
          }];
        }
    
        else{
          result(FlutterMethodNotImplemented);
        }
    }];
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    

    To clarify, the bottom set of code written in objective-c is definitely not correct, but I've been trying to piece it out. I have the images path and I am trying to upload it to Dropbox. Main issue is that the filePath is not reaching the method channel. NSString *filePath = call.arguments(@"filePath") isn't an actual function call.

  • Haroon khan
    Haroon khan over 4 years
    You just saved me a lot of time. Thank you very much! I was looking for something like this for many days, trying different solutions and just resolved what I was stuck in through your answer.