'Null' is not a subtype of type 'LoginController' in type cast in GETX Flutter - flutter-state

I am using nullsafety and following code produce error . The error is type 'Null' is not a subtype of type 'LoginController' in type cast.
home: Obx(() {
if (controller.authState == "Authenticated") {
return HomeView();
} else {
return LoginView();
}
}));
Please suggest a correct approach.

Initialize authstate in your getx controller
enum Status { authenticated,notauthorized}
class Controller extends GetxController
{
Rx<Status> status = Status.notauthorized.obs;
}
Or you may not intialized logincontroller
final LoginController logincontroller =
Get.put(LoginController());
in your page

Related

Method overriding rules in Dart

Please look at the code below:
void main() {
Child c = new Child();
c.m1("12");
}
class Parent {
void m1(int a) {
print("value of a $a");
}
}
class Child extends Parent {
#override
void m1(String b) {
print("value of b $b");
}
}
When I run the program, it shows me the following error message:
Error: The parameter 'b' of the method 'Child.m1' has type 'String', which does not match the corresponding type, 'int', in the overridden method, 'Parent.m1'. Change to a supertype of 'int', or, for a covariant parameter, a subtype.
I didn't quite understand this error message. What are the conditions of overriding a method in dart?
Probably I cannot change the data type while overriding the function. Is it?

Make enum with return Class inheriting Codable to parse JSON to your model class

I need to make an enum to return class type to using in another function to parse my JSON to model.
Below follows an example of what I wrote.
enum APIType {
case status
var responseType: AnyClass {
switch self {
case .status:
return MyModel.self
}
}
}
But, one error occurred which I assume is happening due to Codable inheritance.
Cannot convert return expression of type 'MyClass.Type' to return type 'AnyClass' (aka 'AnyObject.Type')
Has anyone gone through this challenge? How do I resolve this?
The error is actually quite clear, you are trying to return the class type of an object, but the compiler expects a concrete implementation of any class.
Just replace the return value of responseType to be AnyObject.Type
class MyModel {
}
enum APIType {
case status
var responseType: AnyObject.Type {
switch self {
case .status:
return MyModel.self
}
}
}
print(APIType.status.responseType) // MyModel

Error: The argument type 'String' can't be assigned to the parameter type 'User'. / Flutter can't assign constructor in class

void main() {
Login('John');
}
class User {
String name;
User(this.name);
}
class Login {
User user;
Login(this.user);
print(user.name);
}
Error: The argument type 'String' can't be assigned to the parameter type 'User'.
How to fix this error or use other use case;
Your Login constructor takes a User as first argument, not a String. You need to construct a User object and provide that to your Login constructor.
void main() {
Login(User('John'));
}
Another issue in your code is the print(user.name); line directly in your Login class definition. That is not allowed. If you want to execute code as part of calling the constructor, you can do that like:
Login(this.user) {
print(user.name);
}
So the full solution is going to look like this:
void main() {
Login(User('John'));
}
class User {
String name;
User(this.name);
}
class Login {
User user;
Login(this.user) {
print(user.name);
}
}

Dart - NoSuchMethodError: Class 'Module' has no instance getter 'String'

Could some please explain what is wrong?
I try to get an item by key from HashMap:
If I do it that way:
var stringProvider = providersHolder[String]
I get an error:
NoSuchMethodError: Class 'Module' has no instance getter 'String'.
If I do it next way:
var stringProvider = providersHolder[providersHolder.keys.last]
where providersHolder.keys.last == String
I get success.
Here is a full listing:
class Module {
HashMap providersHolder = new HashMap<Type, ItemCreator>();
void addProvider<T, M extends Module>(ItemCreator<T, M> itemCreator) {
providersHolder[T] = itemCreator;
}
}
class Test {
Test() {
var module = Module();
module.addProvider((module) => new Ticker());
module.addProvider((module) => "hello");
var ticketProvider = module.providersHolder[Ticker]; //success
var stringProvider = module.providersHolder[String]; //error
}
}
Here is a watcher:
Could you provide more details on how ItemCreator was defined? This will help us replicate the behavior you've encountered. If you're still having the same errors, you can also run flutter doctor -v to check for any errors or warnings.

Calling .filter() on a stream looses generic type

I have the following code. "Protectable" is an interface. My compiler gives the following error: "Incompatible types: Object cannot be converted to Collection"
When I remove the .filter line, everything works. Why does the compiler loose my type here?
Thanks,
Hannes
Collection<Protectable> requiredItems prefs.getConnectedProtectables(fuzDoc)
.stream()
.filter(protectable -> !protectable.itemVisibleForCurrentUser(fuzDoc))
.collect(Collectors.toList());
The variable prefs implements HasConnectedRights which is implemented as follows:
public interface HasConnectedRights {
public Collection<Protectable> getConnectedProtectables(FuzDocument doc);
}
The interface Protectable declares the method itemVisibleForCurrentUser like this:
default public boolean itemVisibleForCurrentUser(Docker<FuzDocument> doc) {
User user = UserCtrl.getCurrentUser(doc.getDoc());
return user == null || itemVisibleFor(user);
}

Resources