简体   繁体   English

dart中如何将String转成char

[英]How to convert String into char in dart

I want to convert string into a character.我想将字符串转换为字符。 I am trying to send Gender M or F to the API but the api only supports char data type.我正在尝试将性别 M 或 F 发送到 API,但 api 仅支持 char 数据类型。 and it is giving error when we try to pass like this.当我们尝试像这样通过时它会出错。

String gender = "M";

Is there any way to convert the string into the character??有没有办法把字符串转换成字符??

In Dart, you can convert a string to list of chars by using the split method.在 Dart 中,您可以使用split方法将字符串转换为字符列表。

var str = "M";
str.split(""); // ['M']

Alternatively, you can also use the charAt method:或者,您也可以使用charAt方法:

var str = "M";
str.charAt(0); // 'M'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM