1

I have an application with a form and now with a little experimental refactoring from last week 2 buttons doesn't do anything.

Logging just say "button was click " but no method is called.

The code is :

Widget build() {
///...
ElevatedButton (
   onPress : genText1(),
   child : Text("add text for image 1")
),
///...
}

An error is detected here in a method called by genText1()

Uint8List? genText2(int index, Uint8List image) {
final appState = Provider.of<AppState>(context, listen: false);
log.info(
  "GenText2 dart method. Starting process... invoke generateText (Kotlin)",
);
log.info(
  "GenText2 dart method. Starting process... Inputs : checks if not null",
);
try {
  var image2 = Image.memory(image);
  var width = image2.width;
  var height = image2.height;
  var imageBytes = image;
  var imageWidth = width;
  var imageHeight = height;

  if (imageWidth != null && imageHeight != null) {
    try {
      final Map<String, dynamic> arguments = {
        'image': imageBytes,
        'width': imageWidth, // <-- ADD THIS
        'height': imageHeight, // <-- ADD THIS
      };
      Future<dynamic> result = _FirstPanelState._channel.invokeMethod(
        'generateText',
        arguments,
      );
      log.info(
        "GenText2 dart method. Starting process... generateText invoked (return to Dart)",
      );
      // Handle cases where imageBytes, imageWidth, or imageHeight might be null
      result.then((value) {
        if (value != null) {
          var value2 = value as Uint8List;
          log.info(
            "GenText2 dart method. treats return value (not null...)",
          );
          setState(() {
            switch (index) {
              case 1:
                log.info(
                  "GenText2 dart method. treats return value set textFileContent1 $value",
                );
                appState.textFileContent1 = value2;
                break;
              case 2:
                log.info(
                  "GenText2 dart method. treats return value set textFileContent1 $value",
                );
                appState.textFileContent2 = value2;
                break;
              case 3:
                log.info(
                  "GenText2 dart method. treats return value set textFileContent1 $value",
                );
                appState.textFileContent3 = value2;
                break;

              default:
            }
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Completed with 100% no error. ')),
            );

            //appState.selectedImage = Image.memory(value);
          });
        }
      }).catchError((error) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Error: $error')),
        );
      });
    } on PlatformException catch (e) {
      log.severe("Failed to generate text: '${e.message}'.");
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Failed to generate text. Error: $e')),
      );
    }
  } else {
    log.info("Error: imageWidth or imageHeight is null.");
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Error: imageWidth or imageHeight is null.')),
    );
  }
} on PlatformException catch (e) {
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('Error: $e')),
  );
  log.fine("Platform Error Code: ${e.code}");
  log.fine("Platform Error Message: ${e.message}");
  log.fine(
    "Platform Error Details: ${e.details}",
  ); // Now will be a String or a Map
  // If you sent a Map, you can process it like this:
  if (e.details is Map) {
    final errorDetails = e.details as Map;
    log.fine("Error Size: ${errorDetails["size"]}");
    log.fine("Error Message: ${errorDetails["message"]}");
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text( "Error Size: ${errorDetails["size"]}\nError Message: ${errorDetails["message"]}"
      ),
    ));
  }
}
return null;

}

Error: imageWidth or imageHeight is null.

2 Answers 2

3

The problem is that you are not correctly calling the genText1() method in the onPressed, the correct way is like:

ElevatedButton(
              child: const Text("Ok"),
              onPressed: () {
                genText1();
              },
            )

or another way of passing methods:

ElevatedButton(
              child: const Text("Ok"),
              onPressed: genText1,
            )
Sign up to request clarification or add additional context in comments.

Comments

0

The main issue with your ElevatedButton is that you are calling the function immediately instead of passing it as a callback. In Flutter, the onPressed property expects a function reference, not the result of a function call.

What you have done:

ElevatedButton(
  onPress: genText1(),  // This calls genText1 immediately when the widget builds
  child: Text("add text for image 1"),
),

What you should do:

ElevatedButton(
  onPressed: genText1,  // Pass the function reference here
  child: Text("add text for image 1"),
),

Corrected image dimension extraction example:

Future<Uint8List?> genText2(int index, Uint8List image) async {
  final appState = Provider.of<AppState>(context, listen: false);
  try {
    final ui.Image decodedImage = await getImageFromBytes(image);
    final int width = decodedImage.width;
    final int height = decodedImage.height;

    final Map<String, dynamic> arguments = {
      'image': image,
      'width': width,
      'height': height,
    };

    final result = await _FirstPanelState._channel.invokeMethod('generateText', arguments);

    if (result != null) {
      final value2 = result as Uint8List;
      setState(() {
        switch (index) {
          case 1:
            appState.textFileContent1 = value2;
            break;
          case 2:
            appState.textFileContent2 = value2;
            break;
          case 3:
            appState.textFileContent3 = value2;
            break;
        }
      });
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Completed with 100% no error.')),
      );
      return value2;
    }
  } catch (e) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Error: $e')),
    );
  }
  return null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.