displayText static method

Future<bool> displayText(
  1. {required String text,
  2. Duration duration = const Duration(seconds: 3)}
)

Displays text on the newPOS screen for a specified period of time.

The function uses a stream to verify if the text was displayed correctly.

Parameters:

  • text: The text to display. Must be a string.
  • duration (optional): The duration in seconds that the text will be displayed on the screen. The default value is 3 seconds.

Returns:

The function returns a Future<bool>. The Future resolves to true if the text was displayed correctly, or false if an error occurred.

Exceptions:

The function throws a FlutterPosException if the text cannot be displayed for any reason.

Example:

// Displays the text "Hello world" on the POS screen for 5 seconds.
final result = await FlutterNewposSdk.displayText(
  text: 'Hello world',
  duration: const Duration(seconds: 5),
);

// Checks if the text was displayed correctly.
if (result) {
  print('The text was displayed correctly');
} else {
  print('An error occurred while displaying the text');
}

Implementation

static Future<bool> displayText({
  required String text,
  Duration duration = const Duration(seconds: 3),
}) async {
  /// Stream method
  final stream = FlutterNewposSdk._methodStream.stream
      .where((m) => m.method == 'OnDisplayTextOnScreenSuccess')
      .map((m) {
    return m.arguments as bool;
  });
  try {
    final methodResult = await _invokeMethod(
      'displayTextOnScreen',
      {
        'message': text,
        'keepShowTime': duration.inSeconds,
      },
    );

    final result = methodResult as bool;

    if (!result) {
      throw const FlutterPosException(
        code: 'DISPLAY_TEXT_EXCEPTION',
        message: 'Exception displaying text without any reason',
      );
    }
    final streamOutput = await getFirstResultInStream(
      stream,
      const Duration(seconds: 2),
    );
    return streamOutput ?? false;
  } catch (e) {
    throw const FlutterPosException(
      code: 'DISPLAY_TEXT_EXCEPTION',
      message: 'Exception displaying text without any reason',
    );
  }
}