Why am I getting Dart Error: Undefined name 'yas'? - flutter

main() {
CarsB cars = CarsB(lastname: 'Uysal', firstname: 'Kerem');
print(cars.firstname);
}
class Cars {
String firstname = '';
String lastname = '';
int yas = 0;
Cars(
this.yas, {
this.firstname = '',
this.lastname = '',
});
}
class CarsB extends Cars {
CarsB({String firstname = '', String lastname = ''})
: super(yas, firstname: firstname, lastname: lastname);
}
Why am I getting an error?
Error is:
Error: Undefined name 'yas'.
main.dart:51
: super(yas, firstname: firstname, lastname: lastname);
^^^
Exited (254)

CarsB({String firstname = '', String lastname = ''})
: super(yas, firstname: firstname, lastname: lastname);
Your CarsB constructor does not have a parameter called yas. So passing yas to the super constructor is a mistake. The compiler does not know what to do with it. When I call final b = CarsB(firstname: 'John', lastname: 'Doe'); what do you expect yas to be? It is undefined. And your compiler cannot compile that.

Related

The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]' for Flutter

I am trying to store my database value into a class, but i was unable to convert it into my class using DataSnapshot. I have already added all necessary null safety operators. But it still shows an error.
class User {
String userID = "";
String name = "";
String phoneNo = "";
String email = "";
String password = "";
User(
{required this.userID,
required this.name,
required this.phoneNo,
required this.email,
required this.password});
User.fromSnapshot(DataSnapshot dataSnapshot) {
userID = dataSnapshot.key!;
if (dataSnapshot.value != null) {
name = dataSnapshot.value!["name"] as String;
email = dataSnapshot.value!['email'];
phoneNo = dataSnapshot.value!['phone'];
password = dataSnapshot.value!['password'];
}
}
}
I am trying to define the snapshot value as a String but also the same as others.
Error message
try
if (dataSnapshot.value != null) {
final data = dataSnapshot.value as Map;
name = data["name"] as String;
email = data['email'] as String;
phoneNo = data['phone'] as String;
password = data['password'] as String;
}
Try to specify the type of your DataSnapshot:
User.fromSnapshot(DataSnapshot<Map<String,dynamic>> dataSnapshot) {
userID = dataSnapshot.key!;
if (dataSnapshot.value != null) {
name = dataSnapshot.value!["name"] as String;
email = dataSnapshot.value!['email'];
phoneNo = dataSnapshot.value!['phone'];
password = dataSnapshot.value!['password'];
}
}

Using value/variable used in if.clause after the if

I want to create a new address model. Doing so, beforehand I check a data structure if this contains specific information and then extract them and store it into a variable. After all if-clauses I want to use these variables to create a new object of type Address. However, all the in-between stored variables are not recognized:
final List type = c['types'];
if (type.contains('street_number')) {
final streetNumber = c['long_name'];
}
if (type.contains('route')) {
final street = c['long_name'];
}
if (type.contains('locality')) {
final city = c['long_name'];
}
if (type.contains('postal_code')) {
final zipCode = c['long_name'];
}
final address = Address(
country: null,
postalCode: zipCode, //Undefinex name 'zipCode'
city: city,
streetNumber: streetNumber,
long: null,
lat: null);
});
Variables which are declared in a code block will be removed after that block. So you have to declare that Variables before those blocks:
dynamic streetNumber;
if (type.contains('street_number')) {
streetNumber = c['long_name'];
}
dynamic street;
if (type.contains('route')) {
street = c['long_name'];
}
dynamic city;
if (type.contains('locality')) {
city = c['long_name'];
}
dynamic zipCode;
if (type.contains('postal_code')) {
zipCode = c['long_name'];
}

Filter with psql and typeorm

I need to implement a filter with multiple parameters. For example, it can be a name, city, age, skills and etc. How can I implement it with query builder? Now I use this construction
const firstName = params.name
? {
firstName: Like(`%${params.name}%`),
}
: {};
const lastName = params.name
? {
firstName: Like(`%${params.name}%`),
}
: {};
const companyName = params.companyName
? { companyName: params.companyName }
: {};
const location = params.location
? {
city: params.location,
}
: {};
const searchData = {
...firstName,
...lastName,
...companyName,
...location,
};
let [users, count] = await this.userRepository.findAndCount({
where: searchData,
});
But I can't use ILIKE and other stuff. I've tried to use query builder, but I couldn't. Could you help me, please?
Your manipulations with name, firstName, lastName a little confuse me and I'm not fully understand what you want to do,
But query builder approach way could looks like:
const { firstName, lastName, companyName, location } = params;
const queryBuilder = this.userRepository.createQueryBuilder('user');
if (firstName) {
queryBuilder.andWhere(`user.firstName LIKE '%${firstName}%'`);
// or maybe queryBuilder.andWhere(`user.name LIKE '%${firstName}%'`);
}
if (lastName) {
queryBuilder.andWhere(`user.lastName LIKE '%${lastName}%'`);
// or maybe queryBuilder.andWhere(`user.name LIKE '%${lastName}%'`);
}
if (companyName) {
queryBuilder.andWhere(`user.companyName = ${companyName}`);
}
if (location) {
queryBuilder.andWhere(`user.city = ${location}`);
}
const [ users, count ] = await queryBuilder.getManyAndCount();

concatenate strings in LINQ query

Suppose I want to set FullName property using string.Format like this:
var userList = users.Select(user => new User()
{
Id = user.Id,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName
FullName = string.Format("{0} {1}", user.UserName, user.FirstName)
}).ToList();
This obviously doesn't work because LINQ doesn't know about string.Format.
My question is what are the other options beside going over the list in memory and setting FullName for each item?
userList.ForEach(u => u.FullName = string.Format("{0} {1}", user.UserName, user.FirstName))
UPDATE: to see what I need, please see my conversation with #octavioccl below
you can use it:
FullName = user.UserName + " " + user.FirstName
But I think that it could be better solution (of cource if it's possible for you):
public class User
{
public int Id {get;set;}
public string UserName {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string FullName
{
get
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
}
then in your query build it:
var userList = users.Select(user => new User()
{
Id = user.Id,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName
}).ToList();
String.Format is not supported in EF, try this way:
FullName = user.UserName + " " + user.FirstName
In this link you will find all CLR methods that are supported
Another object is to project to an anonymous type without that property, then project to the final type in linq-to-objects:
var userList = users.Select(user => new {
Id = user.Id,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName
})
.AsEnumerable()
.Select(user => new User()
{
Id = user.Id,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName
FullName = string.Format("{0} {1}", user.UserName, user.FirstName)
})
.ToList();

Coffeescript Syntax Error

What's wrong here?
ExecJS::ProgramError: Error: Parse error on line 11: Unexpected '.'
.coffee
Person = Ember.Object.extend(
firstName: null
lastName: null
fullName: ->
firstName = #get("firstName")
lastName = #get("lastName")
firstName + " " + lastName
.property("firstName", "lastName")
)
Original .js
Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,
fullName: function() {
var firstName = this.get('firstName');
var lastName = this.get('lastName');
return firstName + ' ' + lastName;
}.property('firstName', 'lastName')
});
You need to add braces when you call the method.
Person = Ember.Object.extend(
firstName: null
lastName: null
fullName: (->
firstName = #get("firstName")
lastName = #get("lastName")
firstName + " " + lastName
).property("firstName", "lastName")
)
It is true that they are optional in Coffeescript, but in this situation I think you need to explicitly add them. At least it was the only way I could get it to compile. Perhaps a Coffeescript expert can enlighten as to why.