Why am I getting an error while trying to run this on dart? - flutter

the operator '+' can't be unconditionally invoked because the receiver can be null.
var numbers = <int>[1,3,2,5,4];
var initialValue = 10;
void main() {
var foldNum = numbers.fold(initialValue, (curr, next) => (curr + next));
print(foldNum);
}

Specify the type of fold as fold<int>...
var numbers = <int>[1,3,2,5,4];
var initialValue = 10;
void main() {
var foldNum = numbers.fold<int>(initialValue, (curr, next) => (curr + next));
print(foldNum);
}

Related

How to save operators in Dart

I want to do something like this in dart.
var z = +
and then put it in another var like this
var x = 5 z 5
And when I did this
var z = +
I get an Error
Expected an identifier.
you can not make variable operators in dart though what you can do is to create a custom variable for the same by doing:
void main() {
var operators = {
'+': (a, b) { return a + b; },
'<': (a, b) { return a < b; },
// ...
};
var op = '+';
var x = operators[op]!(10, 20);
print(x);
}

error: Conditions must have a static type of 'bool'

I tried to make a calculator in flutter for the first time, but an error occur at the bool type that i don't understand. (on the "if" opertaion)
class _CalculatorAppState extends State<CalculatorApp> {
int? firstNum;
int? secondnum;
String? textToDisplay;
String? history = '';
String? res = '';
String? operation;
void btnOnClick(String btnVal){
print(btnVal);
if( btnVal == 'C'){
textToDisplay = '';
firstNum = 0;
secondnum = 0;
res= '';
} else if (btnVal == 'C'){
textToDisplay = '';
firstNum = 0;
secondnum = 0;
res= '';
history = '';
} else if ( btnVal == '+'||
btnVal == '-'||
btnVal == '/'||
btnVal == 'X') {
firstNum = int.parse(textToDisplay!);
res = '';
operation = btnVal;
} else if (btnVal == '='){
secondnum = int.parse(textToDisplay!);
if(operation = '+') {
res = (firstNum! + secondnum!).toString();
history = firstNum.toString() + operation.toString() + secondnum.toString();
}
if(operation = '-') {
res = (firstNum! - secondnum!).toString();
history = firstNum.toString() + operation.toString() + secondnum.toString();
}
if(operation = 'X') {
res = (firstNum! * secondnum!).toString();
history = firstNum.toString() + operation.toString() + secondnum.toString();
}
if(operation = '/') {
res = (firstNum! / secondnum!).toString();
history = firstNum.toString() + operation.toString() + secondnum.toString();
} else {
res = int.parse(textToDisplay! + btnVal).toString();
}
setState(() {
textToDisplay = res;
});
}
}
error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:46)
error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:50)
error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:54)
error: Conditions must have a static type of 'bool'. (non_bool_condition at [calculator] lib\main.dart:58)
You're currently typing operation = 'x' which isnt valid syntax. You need a double '=', so:
if (operation == 'x') {
// do stuff
}
The equality operator is == not =.
At those lines listed you used = instead of ==.
And declare the variables as follows:
var firstNum = 0;
var secondnum = 0;
var textToDisplay = '';
var history = '';
var res = '';
var operation = '';

System.Linq.Queryable.Except is not supported in Entity Framework Core

Please note this error message :
System.NotSupportedException: 'Could not parse expression
'value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable
This overload of the method 'System.Linq.Queryable.Except' is
currently not supported.'
Is supported in newer versions of ef core?
my code :
var tuple = SearchBrand(searchTerm);
var result = GetExceptionsBrand(tuple.Item1, categoryId);
return Json(new
{
iTotalDisplayRecords = tuple.Item2,
iDisplayedBrand = result
.Skip(page * 10)
.Take(10)
.ToList(),
});
public async Task<Tuple<IQueryable<BrandDto>, int, int>>SearchBrand(string searchTerm)
{
var result = _context.Brands
.Where(c => c.IsDeleted == displayIsDeleted)
.WhereDynamic(searchTerm)
return new Tuple<IQueryable<BrandDto>, int, int>(result,
filteredResultsCount, totalResultsCount);
}
public IQueryable<BrandDto> GetExceptionsBrand(IEnumerable<BrandDto> filteredBrand, int categoryId)
{
var query = _context.CategoriesBrands.Where(x => x.CategoryId == categoryId);
var selectedList = new List<BrandDto>();
foreach (var item in query)
{
var cb = new BrandDto()
{
BrandDto_BrandId = item.BrandId
};
selectedList.Add(cb);
}
IQueryable<BrandDto> ExcpetList = filteredBrand.AsQueryable().Except(selectedList, new ComparerBrand());
return ExcpetList;
}

Trying to Move Cards

I am trying to move a card I have found using the search function from one list to another on the same board.
I have tried to set up a new list using the a new factory.list and searching for the list using FirstorDefault LINQ Query but keep getting an Error.
Below is some code I have tried to move my cards.
string query = jnum;
var search = factory.Search(query, 1, SearchModelType.Cards, new IQueryable[] { board });
await search.Refresh();
var CardList = search.Cards.ToList();
foreach (var card in CardList)
{
string tName = card.Name.Substring(0, 6);
if (tName == jnum.Trim())
{
cardid = card.Id;
}
}
var FoundCard = factory.Card(cardid);
string FoundListid = FoundCard.List.Id;
var fromlist = factory.List(FoundListid);
if (Item != null)
{
if (mail.Body.ToUpper().Contains("Approved for Print".ToUpper()))
{
//var ToList = factory.List("5db19603e4428377d77963b4");
var ToList = board.Lists.FirstOrDefault(l => l.Name == "Signed Off");
FoundCard.List = ToList;
// from on proof
//MessageBox.Show("Approved for Print");
}
else if (mail.Body.ToUpper().Contains("Awaiting Review".ToUpper()))
{
//var ToList = factory.List("5db19603e4428377d77963b3");
var ToList = board.Lists.FirstOrDefault(l => l.Name == "On Proof");
FoundCard.List = ToList;
// from in progress or to start
// MessageBox.Show("Awaiting Review");
}
else if (mail.Body.ToUpper().Contains("Amends".ToUpper()))
{
var ToList = factory.List("5dc9442eb245e60a39b3d4a7");
FoundCard.List = ToList;
// from on proof
//MessageBox.Show("Amends");
}
else
{
// non job mail
}
}
I keep getting a is not a valid value Error.
Thanks for help

Entity Framework. Where clause with parameter from query as variable

Task: use different where clause in one query
Here is example (it is not real query, just to illustrate the problem)
var events = ctx.Events; // ctx - EntityFramework context
var res = events
.GroupBy(ee => ee.State)
.Select(gg => new
{
State = gg.Key,
FirstTwo = events
// how to get this clause from variable
.Where(ee => ee.State == gg.Key)
.Take(2)
})
.ToList();
Next code did not work, the problem is that where expression use parameter from query gg.Key
var events = ctx.Events;
var res = events
.GroupBy(ee => ee.State)
.Select(gg => new
{
State = gg.Key,
FirstTwo = events
// 1
// how to get this clause from variable
//.Where(ee => ee.State == gg.Key)
// 2
// try to take out where expression from query
.Where(_buildExpression(gg.Key))
.Take(2)
})
.ToList();
// method
static Expression<Func<Event, bool>> _buildExpression(string state)
{
return ee => ee.State == state;
}
// exeption
An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll
Additional information: variable 'gg' of type 'System.Linq.IGrouping`2[System.String,Entities.Event]' referenced from scope '', but it is not defined
Example of getting where expression from variable, but does not depend on gg.Key (wrong)
Expression<Func<Event, bool>> whereClause = (ee) => (ee.State == "test");
var events = ctx.Events;
var res = events
.GroupBy(ee => ee.State)
.Select(gg => new
{
State = gg.Key,
FirstTwo = events
// 1
// how to get this clause from variable
//.Where(ee => ee.State == gg.Key)
// 2
// try to take out where expression from query
//.Where(_buildExpression(gg.Key))
// 3
// whereClause from variable, but does not depend on gg.Key
.Where(whereClause)
.Take(2)
})
.ToList();
How to take where сlause from variable with depend on gg.Key?
p.s. the query is just example of the problem. The code below does not solve the problem of real query:
var events = ctx.Events;
var res = events
.GroupBy(ee => ee.State)
.Select(gg => new
{
State = gg.Key,
FirstTwo = gg.Take(2)
})
.ToList();
Solution by OP.
Thanks to Ivan Stoev comment.
Expression<Func<Event, string, bool>> whereClause = (ee, state) => (ee.State == state);
var events = ctx.Events;
var res = events
.AsExpandable() // <= add this
.GroupBy(ee => ee.State)
.Select(gg => new
{
State = gg.Key,
FirstTwo = events
.Where(ee => whereClause.Invoke(ee, gg.Key)) // <= Invoke expression
.Take(2)
})
.ToList();
This was made possible by LinqKit