I have a navigation bar that leads me to the class "Busca". A second class "_BuscaSatet" already extends "Busca".
How can i make 2 classes to extend "Busca" as i created a third one "Categorias"
I am new to dart, tried doing this:
class Busca extends StatefulWidget {
#override
_BuscaState createState() => _BuscaState();
_CategoriaState createState() => _CategoriaState();
}
and then
class _CategoriaState extends State<Busca> {
If you're looking to provide "generic" return types for a single method, you might try the following, which allows you to specify the intended return type with generics on the function level rather than at the class level:
class MyClass {
T createState<T>(...) {
...
return T(...);
}
}
Then you might call it as
MyClass myClass = MyClass();
OtherClass other = myClass.createState<OtherClass>(...);
Related
I can't understand why this code doesn't compile.
In my intentions, the following snippet should declare a BaseGenericClass with no constraint on its argument, and a GenericClass deriving from BaseGenericClass with a constraint on type T telling the compiler to accept only classes derived from AbstractArgClass.
abstract class BaseGenericClass<T> {
final T _arg;
BaseGenericClass(this._arg);
}
class GenericClass<T extends AbstractArgClass> extends BaseGenericClass<T> {
GenericClass() : super(ArgClass());
}
abstract class AbstractArgClass {}
class ArgClass extends AbstractArgClass {}
ArgClass derives from AbstractArgClass, still lines 8 raises this error:
The argument type 'ArgClass' can't be assigned to parameter type 'T'
I think the compiler is correct here. ArgClass and AbstractArgClass in fact do not extend T, and I can't see a way to tell them that T is meant to be ArgClass.
It should work if you change it like this:
class GenericClass<T extends AbstractArgClass> extends BaseGenericClass<T> {
GenericClass(T t) : super(t);
}
You have to supply your instance of ArgClass to the constructor of GenericClass:
final g = GenericClass(ArgClass());
If you do not want to provide this from ouside, you can add a static function to make a new instance, like:
class GenericClass<T extends AbstractArgClass> extends BaseGenericClass<T> {
GenericClass._(T t) : super(t);
static GenericClass make() => GenericClass._(ArgClass());
}
and get an instance:
final g = GenericClass.make();
Ber's answer apparently works around the problem introducing a sort of intermediation... I tried to create a useless IntermediateClass that just introduces the restrictions on the type T (T has to derive from AbstractArgClass). This works!!!
AnotherGenericClass, as expected, does not compile because AnotherClass does not extend AbstractArgClass.
Still the question: why?
It looks like a compiler glitch to me, do you agree?
abstract class BaseGenericClass<T> {
final T _arg;
BaseGenericClass(this._arg);
}
class IntermediateClass<T extends AbstractArgClass> extends BaseGenericClass<T> {
IntermediateClass(T arg) : super(arg);
}
class GenericClass extends IntermediateClass {
GenericClass() : super(ArgClass());
}
abstract class AbstractArgClass {}
class ArgClass extends AbstractArgClass {}
class AnotherClass {}
class AnotherGenericClass extends IntermediateClass {
AnotherGenericClass() : super(AnotherClass());
}
I am looking for the dart syntax to achieve the following but do not know what the correct terminology is to search for the answer.
class BaseClass <T extends MyType> {
List<T> items;
T getFirstItem() => items.first;
}
and then be able to Sub class like this
class ClassForMyType<MyType> extends BaseClass {
List<MyType> items;
MyType getFirstItem() => items.first;
}
Where ClassForMyType would extend BaseClass so that I do not have to re-implement the contrived getFirstItem() method.
Effectively I want to be able to use it like this:
ClassForMyType container = ClassForMyType();
MyType item = Mytype();
container.items.add(item);
List<MyType> itemsFromContainer = container.items;
MyType firstItem = container.getFirstItem();
I have tried something like this:
BaseClass<T extends MyType> {
List<T> items;
void addItemFromMap(Map map) {
items.add(T.fromMap(map));
}
}
The above fails on the .fromMap() which does exist on MyType. In other methods where I access other methods on MyType these appear to work, it seems to have a problem only wiht the named contructor.
I think the only way that would work and look relatively OK would be:
abstract class BaseClass<T extends MyType> {
List<T> items;
// abstract factory method
T itemFromMap(Map map);
void addItemFromMap(Map map) {
items.add(itemFromMap(map));
}
}
class ClassForMyType extends BaseClass<MyType> {
// implementing abstract factory method, delegating call to constructor
MyType itemFromMap(Map map) => MyType.fromMap(map);
}
I agree, a little bit overhead that you should implement factory method in every subclass, but that is the only proper way.
Is there a way to have some sort of multiple inheritance in Dart? I can't use Mixins because the "base" class must then inherit from Object (in my case the "base" class needs to extend ChangeNotifier). Something like protocol oriented programming?
Example:
import 'package:flutter/material.dart';
class BaseModel extends ChangeNotifier {
bool _busy = false;
bool get busy => _busy;
void setBusy(bool value) {
_busy = value;
notifyListeners();
}
}
Because BaseModel extends ChangeNotifier, one can't say:
class A with BaseModel - wrong
You can mostly say class A extends BaseModel, but I am trying to avoid inheritance.
I have a class QuizPage extends StatefulWidget,
and another class _QuizPageState extends State< QuizPage>
class QuizPage extends StatefulWidget {
var HERE; // or over there ?
#override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
var HERE; // or over there ?
#override
Widget build(BuildContext context) {return Container();}
}
What's the difference if any?
I'm new to flutter, but I noticed through the
examples I encountered that if I want to send anything through the
constructor, it has to be in the StatefulWidget and I can access it
inside the State by widget.anything
Variables inside the StatefulWidget subclass cannot change over time. They must be declared as final, or else you'll have a warning.
Adding variables here should be done solely for parameters: Other widgets communicating with this one.
State<T> subclass has much more freedom on that topic. This is where you should put all your internal variables. Whether the change over time or not.
I have a class I want to use mostly as a base class for other classes that have constant instances, but also as a mixin class for others. Ideally, I'm after something like the following:
class Base{
someMethod(){
//do something
}
}
class ConstantClass extends Base{
const ConstantClass();
anotherMethod(){
//do something else
}
}
class MixedClass extends NonMixinClass with Base{
thirdMethod(){
//a third thing
}
}
The above has an error in that ConstantClass cannot have a constant constructor without calling a super-class constant constructor. However, if I add a constructor of any kind to Base(), it can't be used as a mixin.
My current workaround is to duplicate functionality in static methods, as in the following:
class Base{
const Base();
static someStaticMethod(Base base){
//do something
}
someMethod() => Base.someStaticMethod(this);
}
class ConstantClass extends Base{
const ConstantClass(): super();
anotherMethod(){
//do something else
}
}
class BaseMixin implements Base{
someMethod() => Base.someStaticMethod(this);
}
class MixedClass extends NonMixinClass with BaseMixin{
thirdMethod(){
//a third thing
}
}
This isn't too bad when there's only one function in the base class, but things get pretty verbose for a complex class, and if there's a simple way of getting around the problem I'd love to keep things clean. Thanks in advance.
I though class ConstantClass extends Object with Base { would do it, but the new error I get is "Constant constructor cannot be declared for a class with a mixin". So I don't see a solution here, but not using a const constructor.