getDetailedInfo static method

Future<Map<String, dynamic>> getDetailedInfo(
  1. String smiles
)

Get detailed information about a molecule from its SMILES string using PubChem

Implementation

static Future<Map<String, dynamic>> getDetailedInfo(String smiles) async {
  try {
    final encodedSmiles = Uri.encodeComponent(smiles);
    final url = Uri.parse('$chempubBaseUrl/compound/smiles/$encodedSmiles/JSON');

    final response = await http.get(url);
    if (response.statusCode != 200) {
      return {'error': 'Failed to retrieve data from PubChem'};
    }

    final data = jsonDecode(response.body);
    if (data['PC_Compounds'] == null || data['PC_Compounds'].isEmpty) {
      return {'error': 'No compound data found'};
    }

    // Extract useful information
    final compound = data['PC_Compounds'][0];
    final result = <String, dynamic>{
      'cid': compound['id']['id']['cid'],
    };

    // Extract properties
    if (compound['props'] != null) {
      for (final prop in compound['props']) {
        if (prop['urn'] != null && prop['urn']['label'] != null) {
          final label = prop['urn']['label'];
          if (label == 'IUPAC Name' && prop['value'] != null && prop['value']['sval'] != null) {
            result['iupacName'] = prop['value']['sval'];
          } else if (label == 'Molecular Formula' && prop['value'] != null && prop['value']['sval'] != null) {
            result['molecularFormula'] = prop['value']['sval'];
          } else if (label == 'Molecular Weight' && prop['value'] != null && prop['value']['fval'] != null) {
            result['molecularWeight'] = prop['value']['fval'];
          }
        }
      }
    }

    return result;
  } catch (e) {
    return {'error': 'Error fetching detailed information: $e'};
  }
}