Reputation: 935
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'
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?
Upvotes: 1
Views: 2767
Reputation: 1
Firstly declare:
List split(Pattern pattern) {}
outside the class,Now-
String data="user1 @ 186.53";
String edata= data.split(" ").elementAt(0);
print(edata);
output: user1
Upvotes: 0
Reputation: 26
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.
Upvotes: 1
Reputation: 51206
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
Upvotes: 3