fetchMoleculeImage static method

Future<Uint8List> fetchMoleculeImage(
  1. String smiles, {
  2. int width = defaultWidth,
  3. int height = defaultHeight,
})

Fetches a PNG image of the molecule from PubChem

Returns the raw bytes of the PNG image that can be displayed or saved

Implementation

static Future<Uint8List> fetchMoleculeImage(String smiles, {int width = defaultWidth, int height = defaultHeight}) async {
  if (!isSmilesValid(smiles)) {
    throw ArgumentError('Invalid SMILES string provided');
  }

  final encodedSmiles = Uri.encodeComponent(smiles);
  final url = Uri.parse('$pubchemVisualizationUrl/$encodedSmiles/PNG?width=$width&height=$height');

  final response = await http.get(url);
  if (response.statusCode != 200) {
    throw Exception('Failed to fetch molecule image: ${response.statusCode}');
  }

  return response.bodyBytes;
}