How to put multiple text parameter on the flutter (dart)? - dart

I have code like this on the flutter, I need to put parameters data like icon and text, but I got error when put two text, how I can put two type same parameter?
new myCard(icon: Icons.home, text: 'Home', text: '234')

You cannot use the same names for multiple parameters, because they worklike variables for the constructor/method.
What you have to do is create multiple parameters for every text you need. For instance: if you need a title, create a title parameter, for a subtitle, create a subtitle parameter, instead of trying to use title twice.
Another way would be to pass an array of strings if you'll use them together, like to generate a string (check the example below). But don't use it if you want different strings in different places, it wouldn't be a good practice.
Check out this example:
void main(){
print( new Test(title: "Home", description: "The home page", textList: ["a", "list", "of", "strings"]) );
}
class Test {
String title;
String description;
List<String> textList;
Test({String this.title, String this.description,
List<String> this.textList});
#override
String toString() => "Title: " + title + "\nDescription: "
+ description + "\nText list together: " + textList.join(" ");
}
(copy and paste the code in the Dartpad if you want to test it, it simply creates and outputs a test class that receives two string parameters and a list of strings)

Related

Groovy - Append values to variables in a string

Lets assume I have a domain class called Template that looks somewhat like this:
class Template{
String subject
...
}
I save an instance of this class:
Template t=new Template(subject:'Hello ${name}').save()
Now, I fetch this instance inside a method as follows:
def render(Long id){
String name='foo'
Template t= Template.get(id)
println t.subject
}
I want the "println t.subject" to be printed as "Hello foo". What I get is "Hello ${name}".
I want t.subject to dynamically replace the value of variable name - "foo" in place of ${name}.
Can this be achieved in groovy?
I cannot find any documentation of how to do this, or why this cannot be done.
Update:
I tried this on my GroovyConsole.
class Entity{
String name
}
class Template{
String name
String subject
}
String renderTemplate(Template template, Entity entity){
return template.subject
}
Entity e = new Entity(name:'product')
Template template=new Template(name:'emailTemplate',subject:'Product ${e.name}')
renderTemplate(template,e)
The Output I got was:
Result: Product product
class Template {
String subject
// ...
}
Template t = new Template(subject: 'Hello, ${name}').save()
Important: Use single quotes in 'Hello, ${name}' or you will get an error.
def render(Long id) {
String name = "world"
Template t = Template.get(id)
​def engine = new groovy.text.GStringTemplateEngine()
def subject = engine
.createTemplate(t.subject)
.make(name: name)​
println subject
}
There are a couple of things wrong with the code shown. You have this:
class Template{
String subject
...
}
Then you have this:
Template t=new Template(subject:"Hello ${name}").save()
The Groovy String assigned to the subject property will be evaluated as soon as you assign it to subject because subject is a String. The actual value will depend on the value of the name property that is in scope when that code executes.
Later you have this:
def render(Long id){
String name="foo"
Template t= Template.get(id)
println t.subject
}
It looks like you are wanting that local name property to be substituted into a Groovy String that has been assigned to t.subject, but that isn't how Groovy works. t.subject is not a Groovy String. It is a String.
Separate from that you comment that when you println t.subject that the output is "Hello ${name}". I don't think that is possible, but if that is what you are seeing, then I guess it is.
You can use a transient property to emulate the required behavior:
class Template{
String subject
String getSubjectPretty(){ "Hello $subject" }
static transients = ['subjectPretty']
}
Then you can use:
println Template.get(1).subjectPretty

Unable to assert that list contains a map

I want to use hamcrest to compare a list which has map entries.
Map<String, String> aMapWithCertainEntries = new HashMap();
aMapWithCertainEntries.put("entry1Key", "entry1Value");
aMapWithCertainEntries.put("entry2Key", "entry2Value");
List<Map<String,String>> listToTest = Arrays.asList(new Map[] {aMapWithCertainEntries});
//I want to assert that list has a map with entry1Key, entry2Key keys and corresponding values
assertThat(listToTest, hasItem(??))
System.out.println();
In the place marked ?? I want to create the right matcher to assert that my map contains specific keys and values.
Any help/pointers will be greatly appreciated.
If you simply want to assert that the List contains a Map whose entire contents are as expected, the easiest approach is:
Map<String, String> expectedEntries = ....;
assertThat(listToTest, hasItem(expectedEntries));
However, if you want to ensure that the List contains a Map which contains the given subset of entries, you will need to take one of the following approaches:
Approach 1: Create a Custom Matcher
assertThat(listToTest, hasItem(new CustomTypeSafeMatcher<Map<String,String>>("an entrySet that contains " + expectedEntries.entrySet()) {
#Override
protected boolean matchesSafely(Map<String, String> o) {
return hasItems(expectedEntries.entrySet().toArray()).matches(o.entrySet());
}
}));
Approach 2: Assert on each Map's entrySet rather than on the Maps themselves
assertThat(listToTest.stream().map(Map::entrySet).collect(Collectors.toList()),
hasItem(hasItems(expectedEntries.entrySet().toArray(new Map.Entry[expectedEntries.size()]))));

Creating a helper for displaying vertical characters

I would like to create a helper to return a text one character below the other. Something like that:
S
A
M
P
L
E
The purpose of this helper is to have a table with a heading of only 1 character wide. As you can see on the picture below this is ugly:
Example below looks nice:
I would like something like:
#Html.DisplayVerticalFor(x => x.MyText)
Any idea?
Thanks.
You can create an HTML helper DisplayVertical. (I am not adding the steps of how to create html helpers). The DisplayVertical will first split your text in character array and wrap each character inside a div or any other block level element, which can be inserted desired place.The implementation of DisplayVerticalFor can be something like this :
public static MvcHtmlString DisplayVertical (this HtmlHelper helper, string text)
{
string OutputString = "";
string assembleString = "<div>{0}</div>";
char[] textarr = text.ToCharArray();
foreach( char a in textarr )
{
OutputString += String.Format(assembleString, a);
}
return new MvcHtmlString(OutputString);
}
and in razor it will placed like this :
<div class="style-to-adjust-width-n-height"> #Html.DisplayVertical ("Sample") </div>
If you want to pass on a lambda expression to this html helper like this #Html.DisplayVerticalFor(x => x.MyText) then you need to add lambda expression parsing code to find out the text.
Lastly, this is a very rough code however you can add "TagBuilder" etc to make it more neat and clean.

help with oauthService and linkedin

I am trying to iterate over a list of parameters, in a grails controller. when I have a list, longer than one element, like this:
[D4L2DYJlSw, 8OXQWKDDvX]
the following code works fine:
def recipientId = params.email
recipientId.each { test->
System.print(test + "\n")
}
The output being:
A4L2DYJlSw
8OXQWKDDvX
But, if the list only has one item, the output is not the only item, but each letter in the list. for example, if my params list is :
A4L2DYJlSwD
using the same code as above, the output becomes:
A
4
L
2
D
Y
J
l
S
w
can anyone tell me what's going on and what I am doing wrong?
thanks
jason
I run at the same problem a while ago! My solution for that it was
def gameId = params.gameId
def selectedGameList = gameId.class.isArray() ? Game.getAll(gameId as List) : Game.get(gameId);
because in my case I was getting 1 or more game Ids as parameters!
What you can do is the same:
def recipientId = params.email
if(recipientId.class.isArray()){
// smtg
}else{
// smtg
}
Because what is happening here is, as soon as you call '.each' groovy transform that object in a list! and 'String AS LIST' in groovy means char_array of that string!
My guess would be (from what I've seen with groovy elsewhere) is that it is trying to figure out what the type for recipientId should be since you haven't given it one (and it's thus dynamic).
In your first example, groovy decided what got passed to the .each{} closure was a List<String>. The second example, as there is only one String, groovy decides the type should be String and .each{} knows how to iterate over a String too - it just converts it to a char[].
You could simply make recipientId a List<String> I think in this case.
You can also try like this:
def recipientId = params.email instanceof List ? params.email : [params.email]
recipientId.each { test-> System.print(test + "\n") }
It will handle both the cases ..
Grails provides a built-in way to guarantee that a specific parameter is a list, even when only one was submitted. This is actually the preferred way to get a list of items when the number of items may be 0, 1, or more:
def recipientId = params.list("email")
recipientId.each { test->
System.print(test + "\n")
}
The params object will wrap a single item as a list, or return the list if there is more than one.

Render multiple spaces in var

I am trying to add multiple spaces in my var but it get's cut down to one space or it renders out & nbsp; as is. I have tried using & nbsp; and %20 any one have any other ideas?
ViewBag.Subheading = "Bringing to light";
I want it to look like this
Bringing to light
ViewBag.Subheading = "Bringing to light".Replace(" ", " ");
And
#Html.Raw(ViewBag.Subheading)
Or you could do something like:
public static MvcHtmlString DisplayAndRetainSpaces(this HtmlHelper html, string value)
{
return MvcHtmlString.Create(value.Replace(" ", " "));
}
Then call it like:
#Html.DisplayAndRetainSpaces(ViewBag.Subheading)
Use the entity for that: for each space you want.
EDIT: If you already tried and didn't work, there's an helper for outputing html, which should work with the entity:
#MvcHtmlString.Create(" ");

Resources