As we know we can use ternary operator like ? and : in Dart for control flow, like :
String name = 1 == 1 ? "Jhon" : "Ryan";
Is there anyway to make a parameter compulsory on condition ?
The Codes Below Did Not Work, It's Here Just For The Example
class Person{
final String name;
final int age;
const Person({ this.name, name != null ? #required this.age : this.age});
}
No you cannot do this.
You can have asserts checking conditions, but that would be runtime, not compile-time.
Your best way to make it happen is indeed having different named constructors for different purposes.
With the coming null-safety feature it would be a nightmare anyway. Not necessarily for the compiler, but for the programmer who has to understand it.
Annotations like #required can only be added statically, and not depending on conditions.
To me this looks like a case for asserts as mentioned by #nvoigt
class Person{
final String name;
final int age;
const Person({ this.name, this.age}) : assert(name != null || age != null, "either name or age must be provided");
}
Related
please help me solve the following problem.
This part of my code:
class MeditationCard extends StatelessWidget {
const MeditationCard({
required this.title,
required this.image,
this.route});
final String title;
final String image;
final String route;
...
I need the route variable as optional, but when I remove the flag, I get an error and ask me to make the variable mandatory.
Tried different approaches but didn't work for me
This alert dialog
The parameter 'route' can't have a value of 'null' because of its
type, but the implicit default value is 'null'.
This happened because of null safety check.In order to set nullable variable, you should use ?, try this:
final String? route;
You can make it nullable (?) or define default value
Example for nullable
class MeditationCard extends StatelessWidget {
const MeditationCard({
required this.title,
required this.image,
this.route});
final String title;
final String image;
final String? route;
I know you got the proper answer to your question. But I wanted to mention another method to solve this issue that might help you in the future, which is to provide a default value. This way you avoid weird null errors and null checking.
class MeditationCard extends StatelessWidget {
const MeditationCard({
required this.title,
required this.image,
required this.route});
final String title;
final String image;
final String route = 'default route';
}
This way you ensure that the property 'route' has a value and is never null. And you can override it when you need to.
I'm trying to add a positional parameter with default value with GetX in Flutter but I got an error.
import 'package:get/get.dart';
class MainParentModel {
int id;
String name;
RxBool isSelected;
MainParentModel(this.id, this.name, {this.isSelected = false.obs});
}
Error: The default value of an optional parameter must be constant.dartnon_constant_default_value
How can I add this default value?
Thank you
.obs is a getter that returns an Rx<T> of whatever type you add it to. But it's not a constant value so that won't work. You'll notice that if you just use a regular bool and remove .obs the error goes away.
If it has to be type RxBool, then I'm pretty sure your only option is making it required and passing in false.obs whenever you create a new MainParentModel.
MainParentModel(this.id, this.name, {required this.isSelected});
Then a new instance would look like this.
final model = MainParentModel(1, 'Bob', isSelected: false.obs);
Another approach is creating reactive objects where you'll need, I never use classes with reactive variables, for example:
class MainParentModel {
int id;
String name;
bool isSelected;
MainParentModel(this.id, this.name, {this.isSelected = false});
}
In the controller:
Rx<MainParentModel> mainParentModel = MainParentModel().obs;
if you need to rebuild the page when changing isSelected variable:
mainParentModel.update((model){
model.isSelected = true;
});
or
mainParentModel.refresh();
This will trigger rebuild all Obx that you use mainParentModel.value.?
In my Flutter app, I have a class named Task as follows:
class Task {
final String id;
final String title;
final String description;
final DateTime date;
bool isDone;
Task({
required this.id,
required this.title,
this.description = "",
required this.date,
this.isDone = false,
});
}
Now I want to map this class to a database table, means for that I need to map every member of the class with a string to create a table. Like this:
"CREATE TABLE tasks (id TEXT PRIMARY KEY, title TEXT, description TEXT)"
Is there a way in Dart to get a String representation of class members, so to avoid declaring additional static const members just for the names of these members, and also to avoid hardcoding these names every place I need them. Thanks.
You need some reflection, you can get that from the official mirrors library: https://api.dart.dev/stable/2.16.1/dart-mirrors/dart-mirrors-library.html
could anyone help me with how to manage this problem? I am new to Flutter, I am trying to code exactly likes the course's video, but I got this problem:
2. How could I change the 'status' item optional?
class Task {
int id;
String title;
DateTime date;
String priority;
int status; // 0 - Incomplete, 1 - Complete
Task({this.title, this.date, this.priority, this.status});
Task.withId({this.id, this.title, this.date, this.priority, this.status});
It's a null safety problem
solutions
make int nullable
int? id; // preferred
or
when calling constructor use required keyword
Task({required this.id})
or
initialize default value to id
int id = 0;
When you have named parameters constructors
Task({...});
and you do not specify required keyword in front of a variable the variable is optional. But, because your code is written in null-safety way, you must give it value or the other way it would be assigned null (which is illegal in this case).
Nullable var
If you want your value to be null-assignable you can write:
int? status;
Then you have to remember to check if the variable is null or not before you use it.
Default value
Another way is to use default value in the constructor:
Task({..., this.status = 0});
Required value
Or you can force developer to give it a value:
Task({..., required this.status});
Dartz
Another way is to use Option variable from Dartz package:
import 'package:dartz/dartz.dart';
...
Option<int> status;
Task({..., required this.status});
then you can pass none() or some(1) as a status.
Task(..., status: none());
Task(..., status: some(0));
I have a class - Setting. This class looks as follows:
abstract class BaseSetting {
final dynamic value;
final String key;
final bool secure;
final String description;
//...
I also have another class for other kinds of Settings - like numbers, ranges, text, etc. I want the BaseSetting to have a value ofc, (I think), so I tried BaseSetting<T>and the value not being dynamic - but T.
I am not sure if my approach will work but the idea is that EVERY setting has a value, but every child-setting has a fixed type for that value.
(This is my approach to Settings (refresh / minute, language, metric / imperial etc.) by the way - if what I want to achieve is possible but it is a bad way to do so, please comment so I can look at a better approach.)
try to do like this:
abstract class BaseSetting<T> {
final T value;
final String key;
final bool secure;
final String description;
BaseSetting(
this.value,
this.key,
this.secure,
this.description
);
}