Error accessing properties in StatefulWidget - dart

I'm new to flutter. I'm accessing a property-"startingProduct" in a State Class. The variable was defined in A StatefulWidget Class.
But I get "startingProduct is not defined". How do I fix the code?
final String startingProduct; // `StatefulWidget` class
ProductManager(this.startingProduct); // `StatefulWidget` class
_products.add(widget.startingProduct); // `State` class
Error: The getter 'startingProduct' isn't defined for the class 'StatefulWidget'.

In this case, you most likely forgot to specify the type of the State class.
You should use the following syntax:
class _ExampleState extends State<Example> { // in this case `Example` is your StatefulWidget class
To be more clear: I mean that you need to specify the optional type argument T as your StatefulWidget class, e.g. extends State<Example> instead of extends State.

Related

Why doesn't this Dart code with generic classes and constraints compile?

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());
}

How can i extend 2 classes inside one

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>(...);

Dart multiple "inheritance"

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.

Dart Abstract class of Generic Type with Named Constructor

I am attempting to construct an abstract class that requires a named constructor in Dart. Given some Map (m), this generic type must be able instantiate itself.
The Dart compiler is throwing T.fromJson -> Invalid constructor name.
My attempt at coding this:
abstract class JsonMap<T> {
Map toJson();
T.fromJson(Map m);
}
I struggled with the same concept (in the same place ... API parsing :)) ) and I didn't found a proper solution.
But maybe you can use something this thing I found while checking block pattern this (I am not using it for my model part):
abstract class SomeBase {
void load();
}
class Provider<T extends SomeBase> extends InheritedWidget {
final T something;
Provider({
Key key,
#required this.something,
}): super(key: key);
#override
bool updateShouldNotify(_) {
return true;
}
static Type _typeOf<T>() => T;
static T of<T extends SomeBase>(BuildContext context){
final type = _typeOf<Provider<T>>();
Provider<T> provider = context.inheritFromWidgetOfExactType(type);
return provider.something;
}
}
OR just use this without encapsulating it in an inherited widget and provide the already initialised objects (like user or whatever you are parsing) that just load the values from the JSON provided.
You're creating a class named JsonMap that is parameterized on type T. T is not the name of your class, so T.fromJson is not a valid named constructor for JsonMap.
If you want JsonMap to have a named constructor, it should be JsonMap.fromJson(Map m).
Untested, but off the top of my head, you should write your code like so:
abstract class JsonMap<T> {
Map<String, dynamic> toJson();
T fromJson(Map<String, dynamic> m);
}
The dot makes fromJson(Map m) a constructor of type T, or a static function belonging to type T. Without the dot, it is a function belonging to the abstract class JsonMap, returning type T. Specifying the map type is good practice if you know what it will be (like with json).

how to decide if my variables shall be inside the class that extend StatefulWidget OR Starte<>?

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.

Resources