The getter 'value' isn't defined for the type BehaviorSubject<> - flutter

After I updated the code to adapt flutter null safety the code below gaves me the error
The getter 'value' isn't defined for the type
'BehaviorSubject'.
final _brightness = BehaviorSubject<Brightness>();
...
if (_brightness.value == Brightness.light) { ... } // error is here
How to get value from BehaviorSubject then?
Using rxdart: ^0.26.0

This is because the value getter is not implemented by BehaviorSubject class this function belongs to ValueStreamExtensions file which itself is an extension method file on the ValueStream class which BehaviorSubject is implemented by if you wish to use extension function of any type in your code you should first import the extension file into to your code file in this case just add
import 'package:rxdart/src/streams/value_stream.dart';
also, note that for using the BehaviorSubject class your should import that too
import 'package:rxdart/src/subjects/behavior_subject.dart';
or directly import the main Rx library file which imports other dependencies you may wanna use in your code like this " This is the recommended way "
import 'package:rxdart/rxdart.dart';
also, BehaviorSubject itself has a getter which is called valueWrapper that holds the latest value of your subject and you can directly use this getter
here is more information about extension methods in dart
https://dart.dev/guides/language/extension-methods

The Code you provided reads like you want to get the value from a Brightness Instance which is stored in the BehaviorSubject.
https://pub.dev/documentation/rxdart/latest/rx/BehaviorSubject-class.html
A special StreamController that captures the latest item that has been added to the controller, and emits that as the first item to any new listener.
Your _brightness variable is not from type Brightness its of type BehaviorSubject.
So you have to subscribe with a listener to it and you will get the latest value which has been added to the BehaviorSubject.

Related

How to reference a method from one class to another in flutter?

Here I have two classes, one is APIService class which has a function called to register, then another class is AppState which is a provider class, I am using this class for post methods using provider, now when I try to reference this register method in appState I get an error "Try correcting the name to the name of an existing method, or defining a method named 'register' ", I tried importing the class manually but it won't work. What do I do?

#JsonKey Arguments of a constant creation must be constant expressions

i am trying to make MainResponse class which is returned from backend with same format. I have created a Data class and the object it has should be generic json key regarding the class name like below:
import 'package:json_annotation/json_annotation.dart';
import 'package:style/api/response/status_response.dart';
part 'data.g.dart';
#JsonSerializable(genericArgumentFactories: true)
class Data<T> {
#JsonKey(name: T.toString())
T? returnedObject;
}
what I wanted to do is giving JsonKey name value as class name. but i am receiving following error:
Arguments of a constant creation must be constant expressions.
is there a way to achieve it?
sorry, the question was easy. just try to edit classname.g.dart file which is auto generated. and use'T.toString().toLowerCase()'instead of auto generated key.

How does getx realize the `.obs` thing

Learning Flutter/getx package I came across the sample code like the following:
import 'package:get/get.dart';
class MyHomePageController extends GetxController {
final count = 0.obs;
}
The code 0.obs scares me. I mean how an integer can have a .obs attribute? what has the getx pacakge done to my code?
This is a feature of dart language called extension introduced in Dart 2.7, it is a way to add functionality to existing libraries.
You might use extension methods without even knowing it.
For example, when you use code completion in an IDE, it suggests extension methods alongside regular methods.
For example, consider the following code that parses a string into an integer:
int.parse('42')
It might be nice — shorter and easier to use with tools — to have that functionality be on String instead:
'42'.parseInt()
To enable that code, you can import a library that contains an extension of the String class:
import 'string_apis.dart';
// ···
print('42'.parseInt()); // Use an extension method.
Extensions can define not just methods, but also other members such as getter, setters, and operators. Also, extensions have names, which can be helpful if an API conflict arises. Here’s how you might implement the extension method parseInt(), using an extension (named NumberParsing) that operates on strings.
extension NumberParsing on String {
int parseInt() {
return int.parse(this);
}
// ···
}
SUMMARY
the get package use extensions behind the scene to call getter of RxInt.
Object so the attribute is not actually called upon primitive datatype
this is from the source code of the get package ... you can access it by pressing ctrl+ ".obs"
extension IntExtension on int {
/// Returns a `RxInt` with [this] `int` as initial value.
RxInt get obs => RxInt(this);
}

Flutter "argument type not assignable" Error with two identical types

Flutter shows me this error, but the two types are identical.
[CartItem it's just a simple model; there was a conflict since another widget had the same name but I resolved it using "as" in the import statement]
The argument type List<CartItems> (where CartItem is defined in /Users/marco/Documenti Locali/shop_app/lib/providers/cart.dart)' can't be assigned to the parameter type List<CartItems> (where CartItem is defined in /Users/marco/Documenti Locali/shop_app/lib/providers/cart.dart)'.dartargument_type_not_assignable
list.dart(56, 16): List is defined in /Users/marco/flutter/bin/cache/pkg/sky_engine/lib/core/list.dart
cart.dart(3, 7): CartItem is defined in /Users/marco/Documenti Locali/shop_app/lib/providers/cart.dart
list.dart(56, 16): List is defined in /Users/marco/flutter/bin/cache/pkg/sky_engine/lib/core/list.dart
cart.dart(3, 7): CartItem is defined in /Users/marco/Documenti Locali/shop_app/lib/providers/cart.dart
Peek Problem (⌥F8)
No quick fixes available
It was a double slash in the import statement that generated this strange error.
you should use the library prefix in the usage too so if your namespace is
import 'package:app/xxxx/shop_app/lib/providers/cart.dart as shop_cart;
you should specify it in the generic List as well such as;
List shopCarts = etc...;
Check your import statement at the top of file even if you mistyped a directory folder name as 'Caps or Small' you can get this error.

package:analyzer When the parameter type is Function type , type.element is getting 'null'

I am using package:analyzer to do custom static analysis.
when I have dart code like below :
class MyType{
final void Function(MyItemType) callback ;
}
When I try to access the element of the callback type : I am getting null
That is :
parameterElement.type.element evaluates to be null .
Therefore I cannot get the source file of the Types involved in the Function to import those source files.
This issue happens only when I use a Function type that is anything other than just Function().
parameterElement is of Type ParameterElement from analyser package.
My requirement is that I want to import all the files that contain the Types involved in the Function. In my example code shown above , I wish to import the file that contains the MyItemType class.
Also if the return type of the function was some other Type other than void , I want to import the corresponding source file.