I have the following code snippets
.html
<input type = "number"
min = "0"
max = "120"
#yearsCtrl = "ngForm"
[ngFormControl] = "ageForm.controls['yearsCtrl']"
[(ngModel)] = "age.years"
id = "years">
.dart
class Age
{
int years = 0;
}
class AgeComponent {
....
AgeComponent( FormBuilder fb, ModelService
ageSrvc) {
ageForm = fb.group( {
'yearsCtrl': [''],
} );
_yearsCtrl = ageForm.controls['yearsCtrl'];
age = new Age()
}
...
}
My attempts to run the application gives the following errors (partial)
>EXCEPTION: type 'String' is not a subtype of type 'num' of 'value'. in [AST]
EXCEPTION: type 'String' is not a subtype of type 'num' of 'value'. in [AST]
(anonymous function)
ORIGINAL EXCEPTION: type 'String' is not a subtype of type 'num' of 'value'.
(anonymous function)
ORIGINAL STACKTRACE:
(anonymous function)
#0 NumberValueAccessor.writeValue (package:angular2/src/common/forms/directives/number_value_accessor.dart:38:23)
#1 setUpControl (package:angular2/src/common/forms/directives/shared.dart:34:21)
#2 NgFormControl.ngOnChanges (package:angular2/src/common/forms/directives/ng_form_control.dart:111:7)
...
It seems as if the type="num" is not being handled. I suspect the age int might be an issue also, in that it is an int but a string is required. The reverse conversion from sting back to int might also be an issue.
Any help is appreciated.
Thanks
The field needs to be of type String. Angular doesn't convert the value.
Similar to Polymer dart: Data bind integer value to String attribute
See also
How do I parse a string into a number with Dart?
Is there a better way to parse an int in Dart
A custom value accessor might help. See this Plunker for an example.
The component Address implements ControlValueAccessor with writeValue(v), registerOnChange(fn), registerOnTouched(fn)
Related
class Point {
int x;
int y;
Point(this.x, this.y);
Point.zero()
: x = 0,
y = 0;
Point.fromJson({required Map<String, int> json})
:x = json['x'], //Error : `The initializer type 'int?' can't be assigned to the field type 'int'`
y = json['y']; // Error :`The initializer type 'int?' can't be assigned to the field type 'int'`
}
As you can see the argument json here is Map<String , int> so why am getting this error here.When both are non-nullable here ?
Why compiler assuming json['x'] = int? ?
Because the [] operator on Map returns a nullable by spec:
V? operator [](Object? key)
The value for the given key, or null if key is not in the map.
https://api.dart.dev/stable/2.15.1/dart-core/Map/operator_get.html
So if you are asking for a key that is not in your Map you will get a null value back and not an exception.
If you are 100% sure json['x'] will always work and want the application to crash in case this is not the case, you can use json['x']!. Alternative, you need to provide default values or other type of handling in case these values is not in the map.
This error is displayed when I enter this bold code snippet What are the drawbacks?(int)Course.SubGroup??0
public void OnGet(long id)
{
Course= _CourseApplication.GetCourse(id);
var groups = _CourseApplication.GetSarGroup();
ViewData["Groups"] = new SelectList(groups,"Value","Text",Course.GroupId);
var subGrous = _CourseApplication.GetSubGroup(long.Parse(groups.First().Value));
ViewData["SubGroups"] = new SelectList(subGrous, "Value", "Text",**(int)Course.SubGroup??0**);
This error is displayed
error CS0019: Operator '??' cannot be applied to operands of type 'int' and 'int'
The variable on the left side of ?? operator has to be nullable (which means that you can assign null to it), in your case Course.SubGroup should be of type int? not int. And no need cast to int, change like below:
ViewData["SubGroups"] = new SelectList(subGrous, "Value", "Text",Course.SubGroup??0);
I had this weird problem in Dart. Consider the following code :
class Number {
int num = 10;
}
Here, I created a little class with a int object num
When I try to print it using the main() function OUTSIDE the class like :
main() {
print(num);
}
I get the output as :
num
Which is weird, since I expected an error. If I were to print a undefined variable as in print(foo); I would get an error, which is expected.
What I find even more interesting is the runtimeType of a variable whose value is num.
var temp = num;
print(temp.runtimeType);
}
The above code prints _Type, when I expected it to be int.
Can somebody please clear this?
The name num is a type declared in dart:core. It's the supertype of int and double.
When you do print(num); outside the scope where your int num; variable is declared, the num refers to that type from dart:core which is always imported and therefore in scope.
Dart type names can be used as expressions, they evaluate to a Type object.
So, you are printing a Type object for the type num, which prints "num", and the run-time type of that object, which is again a Type object, which prints _Type because that's the actual internal type of the Type object instance.
I am trying to assign to an int value, however, I get this error:
A value of type 'int?' can't be assigned to a variable of type 'int'
The line causing this compile-time error is the following:
int number = someFunction();
The problem here is that the value returned from someFunction is nullable and trying to assign that value to a non-nullable variable is a compile-time error (null-safety was introduced in Dart 2.7).
You need to check for null using a != null condition.
Example
void exampleFunction<T>(T? input) {
T result = input; // <- ERROR here
print(result);
}
In this example, input is nullable and thus cannot be assigned to the non-nullable variable result.
Solution
The given example problem can be solved like this:
void exampleFunction<T>(T? input) {
if (input != null) {
T result = input;
print(result);
}
}
This question already has answers here:
The cast to value type 'Int32' failed because the materialized value is null
(8 answers)
Closed 6 years ago.
I have the following query:
int? Giver = Convert.ToInt32(db.vwAssignments.Sum(a => a.Amount));
but if there is no records that matches the search criteria then the following error will be raised
The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
pls help
The basics of defensive programming:
first assign the result of your Sum() operation to a nullable variable
object resultOfSum = db.vwAssignments.Sum(a => a.Amount);
check for NULL! and only if it's not null, then convert it to an INT
if(resultOfSum != null)
{
int Giver = Convert.ToInt32(resultOfSum);
}
Also: if your Amount field is a Int to start with, most likely this will already give you NULL or a valid Int - I think that call to Convert.ToInt32() is not necessary:
int? Giver = db.vwAssignments.Sum(a => a.Amount);
if(Giver.HasValue)
{
......
}