What can be causing the error shown in the attached picture? - dart

The getter 'documents' isn't defined for the type 'Object'.
Try importing the library that defines 'documents', correcting the name to the name of an existing getter, or defining a getter or field named 'documents'
enter image description here

that's because it cannot know the type of snapshot.data, so you need to cast it to your wanted class. you have to options,
first, use
var userDocument = snapshot.data as UserDocument;
or second, if that's a FutureBuilder, you can define it's type like this: FutureBuilder<UserDocument>

Related

Difference between Object, Dynamic and Var in Dart?

There is a discussion about dynamic and var before null-safety. Then what's the Object? between each of them?
Is Object? == dynamic?
How about var? and dynamic??
Any difference between dynamic? and dynamic?
I see the official document about null-safety, but can't find the related topic.
dynamic is a special type that disables static type-checking. You can attempt to call any method on a dynamic type. If the object turns out not to have such a method, then it will result in a runtime failure instead of a compile-time one.
Object? is a base type suitable for referencing any object, including null. Unlike dynamic, it is statically type-checked, so you would get compile-time failures if you attempt to call most methods on it without explicitly checking the runtime type or without performing a cast.
var? is not valid syntax. var is not a type; it declares a variable without explicitly specifying a type, allowing the type to be inferred.
dynamic? is valid but is redundant. (See #3.)
Variables of type dynamic can already include null, so adding a ? to make it nullable is redundant. The Dart analyzer will tell you so.
3: About dynamic vs dynamic?: they are the same.
Since dynamic also represents nullable types, for the compiler it is the same of dynamic?.
From Why operator ==(Object other) accepts a nullable argument?:
You can also see the analyzer and runtime will call it dynamic even if we check the signature of a method declared to return dynamic?:
void main() {
print(test.runtimeType); //runtimeType of the test function: () => dynamic
}
dynamic? test() { }
In fact a hint of the dart linter reports as 'unnecessary' the use of ? in dynamic? (as in Null?):
The '?' is unnecessary because 'dynamic' is nullable without it. (unnecessary_question_mark).
Personally, I don't understand why dynamic? Is only reported by a hint (which many people, myself included, don't notice) keeping it valid as a syntax.
1: A variable declared with Object? type behaves like all other normal variables whose type is specified, such as String? etc. Since every class -apart Null (the type of null)- is a subclass of Object (and since in Dart there are no primitive values as opposed to objects, unlike in Java; in Dart also null, int and bool are objects. But forget this clarification, if you don't know Java), a variable declared with Object? can contain any value. But the compiler will only allow access -after a null check- to the properties of Object (toString(), ==(), runtimeType, etc).
A variable declared with dynamic (or dynamic?, see point 3) instead allows access to any public member: the compiler will not perform any checks (unless the property begins with an underscore _, because in that case it is clear that it is not public); if you try to access a non-existent member you will instead have an error at runtime Note1. Furthermore, with dynamic we also renounce null safety: dynamic is equivalent to dynamic? (in practice the question mark can be considered implicit, it is as if it were always there).
2: using var, or final -if you want an immutable reference- without declare the type, the compiler check the value assigned to the variable (in fact the omission of the type is not allowed if the variable is not initialized immediately) and treats the variable as if it were declared with that type.
dynamic at runtime:
One use of 'dynamic' that can lead to confusion is with generic classes, because dynamic as parametric type exists also at runtime:
with
dynamic obj = true;
obj at runtime has bool type, but with
List list = [bool];
list at runtime has List<dynamic> type.
However, using
var list2 = [true];
the parametric type is inferred correctly (list2 has List<bool> runtimeType).
Note1 More precisely, a invocation such as myDynamicVariable.nonexistentMember cause an invocation of the noSuchMethod() method on the object; noSuchMethod() can also be overridden and not throw any exception; but this is a rare practice, in Dart 2).

What is the reason behind not able to use this. keyword and datatypes like string and int in named constructor in Dart

Why in named constructor not able to use datatypes and this keyword? It shows Error:
The parameter 'name' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dart(missing_default_value_for_parameter)
Your problem is that, if nothing else is specified, the default value for optional named parameters is null. So what your code actually does right now is setting the variable late String name to null which is not allowed because of the type String. If you want to allow null, the type should be String?.
The late keyword is used in situations where you cannot set the variable when initializing the object and is just a promise to the Dart compiler that the variable will have a value when you are going to use the variable at some point. (the compiler will add a check to make sure the variable is actually given a value before use).
But late is not a way to allow null as a valid value.
Another problem in your code is that it does not really make sense to do:
person({this.name}) {
this.name = name;
}
The reason is that {this.name} is already used for setting the variable name. When you later do: this.name = name then both name is pointing to the same variable in your object.
Based on the rest of your code I think you should remove the late keyword and instead use required to make sure the user of your class are specifying your named parameter. So something like this should work (also, please use Person instead of person for naming classes):
class Person {
String name;
int age;
Person({required this.name, required this.age});
void sayName() => print(name);
}
void main() {
final p = Person(name: 'Mathew', age: 24);
p.sayName();
print(p.age);
}
I removed the named constructor Person.age since I don't think it makes much sense to make a Person without a name. If name and age is truly optional, you can instead change the types to String? and int? and remove the required keyword.
This will make it clear when using your class that these two variable can be empty and the user of the class should remember to check for possible null values.
I recommend reading more about Dart null safety feature starting here: https://dart.dev/null-safety

F# not recognizing members of a record type

I am doing a simple mapping and the compiler doesn't recognize the record's members.
The image shows the type OpeningHours itself is clearly ok, with all 3 properties showing:
But hovering over the marked property shows:
error FS0039: The field, constructor or member 'Day' is not defined.
Namespaces are all referenced and I even assign to the very same properties few lines below without any issue.
OpeningHours is a class, not a record. This is one way to create an instance of the class:
OpeningHours(day = oh.Day, opens = oh.Opens, closes = oh.Closes)
Based on the constructor and properties, it looks like OpeningHours is defined as a class, not a record. The record syntax can't be used with classes, so the solution is either to change OpeningHours to a record, or to instantiate it using its existing constructor.

Type inheritance

In a class unit I have the type
type
TFolderType = (
dirRoot,
dirDatabase,
dirDocs,
dirConfig,
dirBackup,
dirDown,
dirUp,
dirScripts,
dirLicense,
dirImages,
dirMail,
dirProjects,
dirInput,
dirOutput
);
Now I would like to inherit this class from another class and add some extra elements
Is that in any way possible or can I do this in another way
I have thought of creating a class with all the elements as properties but I am not sure if that is the way to go
Like David says, it is not a class.
As an alternative, you can declare your bigger enumeration like:
type
TThingType = (dirThingA, dirRoot, dirDataBase, ..., dirOutput, dirThingY, dirThingZ);
And then declare your TFolderType as:
type
TFolderType = dirRoot..dirOutput;
Some concerns:
A folder 'needs' to be of type thing
The prefix dir 'needs' to be meaningful for both thing and folder
Current code that assumes enumeration index of the TFolderType elements needs to be rewritten, or add all 'new' elements in the back.
That is not class, rather it is an enumerated type. You cannot use inheritance with enumerated types. You will need to create a new enumerated type. Or perhaps solve the problem using something other than an enumerated type.

What role does the "id" in variable(str name, int id) play?

I am using the Rascal library for accessing syntax trees that are produced by the Eclipse Java compiler (JDT.rsc).
I am trying to get a fix on how the abstract syntax tree works. One thing that eludes me is the "variableBinding". Imagine a very simple class MyClass with one method doNothing() containing one statement, a variable declaration myVar. The declaration of the string variable myVar is represented in the AST-snipped below.
Inside the #bindings annotation, under the variableBinding key, there is a list that represents the consecutive components of a path to the variable myVar. The last item represents the actual variable itself, which is represented by the Id constructor variable(str name, int id).
Question: What is the meaning of the id?
It certainly isn't unique, because when I duplicate the method doNothing() and name it doNothing2(), I find variable("doNothing",0) and variable("doNothing2",0) in the AST. What exactly does it identify?
...
variableDeclarationFragment(
"myVar",
none())[
#bindings=(
"typeBinding":entity([
package("java"),
package("lang"),
class("String")
]),
"variableBinding":entity([
class("MyClass"),
method(
"doNothing",
[],
entity([primitive(void())])),
variable("myVar",0) // Here it's 0, but
])
),
#javaType=entity([
package("java"),
package("lang"),
class("String")
]),
#location=|project://my-project/src/MyClass.java|(60,5,<4,15>,<4,19>)
]
...
This id field actually appears to be an identifier assigned internally by Eclipse. You can see the JavaDoc for the IVariableBinding interface here, which is the source of the id:
http://help.eclipse.org/indigo/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/IVariableBinding.html#getVariableId()
Just click on the link for getVariableId() if it doesn't take you there, and instead just takes you to the page for the interface. This is the method called to get the id. From what I can tell, it isn't very useful and can probably safely be ignored in most cases.

Resources