Im trying to pass arguments from my widget's state into a super class, but i cannot access the "widget." from the initialization list.
if i do pass it from the variables and accept an additional parameter in the state's ctor, i get the lint error:
no_logic_in_create_state
Here's my code:
class TransferPage extends View {
final String screenId;
TransferPage(this.screenId, {Key? key}) : super(key: key);
#override
// ignore: no_logic_in_create_state
State<TransferPage> createState() => TransferPageState(screenId);
}
class TransferPageState extends ViewState<TransferPage, TransferController> {
final String screenId;
TransferPageState(this.screenId)
: super(TransferController(GetTransferDataUsecaseParams(screenId)));
I want to pass the id into the "super" ctor,
What's the best way to go about it?
Related
I want to have a StatefulWidget where I can pass the initial value for a non-nullable member of the widgets State from the widgets constructor.
My current solution (see below) seems to be not ideal, I see two problems with it:
The initial value has to be saved in the widget itself before passing it to the state.
The member in the sate has to be marked as late since it can only be set after initialization.
Is there a better way to initialize a StatefulWidget's state non-nullable member from a value passed to the widget constructor?
My current implementation:
class MyWidget extends StatefulWidget {
final String text;
const MyWidget({Key? key, required this.text}) : super(key: key);
#override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
late String text;
#override
void initState() {
text = widget.text;
super.initState();
}
#override
Widget build(BuildContext context) {
return Text(text);
}
}
(Not shown here, but later the text member should be changeable, that's why it is in the State)
hey there your code seems good.
but the better way is using bloc to pass and receive data.
any way . its not necessary to pass and fill data in initstate and _MyWidgetState .
you can receive your data directly in build widget As you wrote (widget.text)
here is some good things for avoid nullable
https://codewithandrea.com/videos/dart-null-safety-ultimate-guide-non-nullable-types/
You could use the constructor of State like this: _MyWidgetState(){ text=widget.text; }. The constructor will certainly be executed before initState and build methods.
I have been working on a flutter project and I have noticed Avoid using private types in public APIs.
Is there a way to fix this warning?
class SubCategoriesPage extends StatefulWidget {
final MainModel mainModel;
// final Ads ad;
const SubCategoriesPage(this.mainModel, {Key? key}) : super(key: key);
#override
_SubCategoriesPage createState() { // Avoid using private types in public APIs.
return _SubCategoriesPage();
}
}
Because createState method return State<Example> so it's preventing returning any private State.
You need to update your code like this.
class SubCategoriesPage extends StatefulWidget {
final MainModel mainModel;
// final Ads ad;
const SubCategoriesPage(this.mainModel, {Key? key}) : super(key: key);
#override
State<SubCategoriesPage> createState() { // Avoid using private types in public APIs.
return _SubCategoriesPage();
}
}
Since this is a StatefulWidget, I'm guessing the _SubCategoriesPage class inherits from State, since it's being returned by createState().
If so, the return type can be changed to State. Since State is public, it can safely be returned from the public createState() method.
Just change _SubCategoriesPage by State
For those using Riverpod, change _SubCategoriesPage by ConsumerState
I had the same issue using this code
class MyPage extends StatefulWidget {
const MyPage({super.key});
#override
_MyPageState createState() => _MyPageState();
}
I tried using this method - State createState() { // Avoid using private types in public APIs.
return _MyPageState();
but than I got this error message - The name MyPageState isn't a type so it can't be used a type argument.
I am trying to apply this state management but i cannot implement it. If I use stateless widget, i can easily implement it but stateful it is complicated and i cannot achieve it. Where to implement cubits? Here is my code part =>
class MaintenanceScreen extends StatefulWidget {
int? locId;
MaintenanceScreen({required this.locId, Key? key}) : super(key: key);
#override
_MaintenanceScreenState createState() => _MaintenanceScreenState();
}
class _MaintenanceScreenState extends State<MaintenanceScreen> {
final colFr = FrenchColors();
if you mean you want to access locId in _MaintenanceScreenState
simply you can use widget.locId inside the build function and flutter will bind this with the widget class.
Trying to make a generic route "base class", where an abstract class defines a getter that returns the route name. Something like this:
abstract class ScreenAbstract extends StatefulWidget {
static String name;
static String get routeName => '/$name';
ScreenAbstract({Key key}) : super(key: key);
}
Then, any "screen" widget can extend this class:
class SomeScreen extends ScreenAbstract {
static final name = 'someScreen';
SomeScreen({Key key}) : super(key: key);
#override
_SomeScreenState createState() => _SomeScreenState();
}
Which should then be accessible like this:
Navigator.of(context).pushNamed(SomeScreen.routeName);
Hoever, when trying that, the linter throws an error:
The getter 'routeName' isn't defined for the type 'SomeScreen'.
What am I doing wrong?
In dart there's no inheritance of static members. See Language Specification here-
Inheritance of static methods has little utility in Dart. Static
methods cannot be overridden. Any required static function can be
obtained from its declaring library, and there is no need to bring it
into scope via inheritance. Experience shows that developers are
confused by the idea of inherited methods that are not instance
methods.
Of course, the entire notion of static methods is debatable, but it is
retained here because so many programmers are familiar with it. Dart
static methods may be seen as functions of the enclosing library.
To tackle this, you can update your solution like this -
Abstract Parent Class -
abstract class ScreenAbstract extends StatefulWidget {
final String _name;
String get routeName => '/$_name';
ScreenAbstract(this._name, {Key key}) : super(key: key);
}
The Screen Widget that extends the Parent class -
class SomeScreen extends ScreenAbstract {
static final String name = "url";
SomeScreen({Key key}) : super(name, key: key);
#override
_SomeScreenState createState() => _SomeScreenState();
}
Then you can access it like this -
Navigator.of(context).pushNamed(SomeScreen().routeName);
I'm creating a stateful widget, and trying to call the superclass construction using super(key: key. However, I'm getting an error saying:
class ArticlesPage extends StatefulWidget {
ArticlesPage({Key key}) {
super(key: key);
}
#override
_ArticlesPageState createState() => _ArticlesPageState();
}
For dart, super should be called in the initializer list. Ex:
ArticlesPage({Key key}) : super(key: key);
If you also want to initialize fields here, you should do this before the super call. super should always be last in the initializer list.
If you want to save key from --> Key key as a class variable also.
In this case, you will get a default GlobalKey value if it's not passed during constructor initialize.
AppStateFullWidget({Key key}):super(key: key ?? GlobalKey()){
_key = super.key;
}