How to send the value of an android variable back to flutter?

Friends, I created a channel between flutter and android to take pictures without user action. And that already works!

I’m getting the image in base64 format. Now I need to get this variable value and send it to flutter. However, the way I’m doing it doesn’t work. I appreciate if anyone can help me!


Here is my code: home.dart

class _HomePageState extends State<HomePage> {
  MethodChannel platform = Constants.platform;
  var isLoading = false;


  Future<Null> _showNativeView() async {
    await platform.invokeMethod('showNativeView');

  Future<dynamic> _handleMethod(MethodCall call) async {
    switch(call.method) {
      case "message":
        return new Future.value("");


import 'package:flutter/services.dart';

const platform = const MethodChannel('');


package com.example.monitoramento;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;


import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    public static final String CHANNEL = "";
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(),
                CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                    Intent i = new Intent(MainActivity.this, DemoCamActivity.class);

The method of the DemoCamActivity.class class looks like this:

    public void onImageCapture(@NonNull File imageFile) {
        // Convert file to bitmap.
        // Do something.
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
        //Display the image to the image view
        ((ImageView) findViewById(;

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("message", encoded);

I ran the code and the string is stored in the variable “encode”. I need to get this variable and send it back to flutter.

I appreciate any comments on how I can do this!

The code above doesn’t give an error, but it returns to the login page and not to home.dart.

1 Like