[英]Get a certain text in a string - flutter
I have a list of strings i want to pass to the next screen based on which item is selected. 我有一个字符串列表,我想根据选择的项目传递到下一个屏幕。 However in the next screen how do i extract only the usernames in the string which is text 'user1' and text 'user2' 但是在下一个屏幕中,我如何仅提取字符串中的用户名,即文本“user1”和文本“user2”
First screen's code that passed the data to second screen: 第一个屏幕将数据传递到第二个屏幕的代码:
List<String> data = [
"user1 @ 186.53",
"user2 @ 23.432",
];
...
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(text: '${items[index]}'),
),
),
Second screen's code that retrieve the data: 第二个屏幕检索数据的代码:
final String text;
SecondScreen({Key key, @required this.text}) : super(key: key);
So the text contained when retrieved is: 因此,检索时包含的文本是:
user1 @ 186.53
how do i only extract the text 'user1' in the string above? 我如何只在上面的字符串中提取文本'user1'?
One way of doing it if @
is present in all string values. 如果@
存在于所有字符串值中,则执行此操作的一种方法。
String val = 'user1 @ 186.53';
List user = val.split('@');
print(user[0]); // output is user1
String rawData = "user1 @ 186.53";
String result = rawData.split(" ")[0];
This code separates the first item in rawData separated by a space and puts it in result. 此代码将rawData中的第一项分隔为空格并将其放入结果中。
首先声明:在类之外List split(Pattern pattern) {}
,Now-
String data="user1 @ 186.53";
String edata= data.split(" ").elementAt(0);
print(edata);
output: user1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.