Using argument in initializer list with flutter - flutter

I want to make a data list page. It needs a reference based on user id passed by argument. However, the argument can't be accessed because it is not static.
I tried to pass reference itself by argument. But it also falls into the same problem.
class DataViewHome extends StatefulWidget{
final String userId;
DataViewHome({this.userId});
#override
State<StatefulWidget> createState() => DataViewHomeState();
}
class DataViewHomeState extends State<DataViewHome>{
final dataReference = FirebaseDatabase.instance.reference().child("users").child(widget.userId);
List<String> dataList = new List();
StreamSubscription<Event> _onDataAddedSubscription;
DataViewHomeState(){
_onDataAddedSubscription = dataReference.onChildAdded.listen(_onDataAdded);
}
_onDataAdded(Event event){
setState(() {
dataList.add(event.snapshot.value);
});
}
}

Related

How can i initialize a string from the result value of a method in flutter?

I'm trying to store the result of a method in a string , but i get errors
String _location(dynamic media){
return media['url'];
}
String myUrl = _location(media);
full class
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
#override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
Future<List<dynamic>> fetchMedia() async {
final result = await http
.get(Uri.parse('https://iptv-
org.github.io/api/streams.json'));
return json.decode(result.body);
}
String _location(dynamic media) {
return media['url'];
}
String myUrl = _location(media);
...
}
The error says The instance member '_location' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression
How can i do this ??
The problem is that I did not find that you have defined and inited the variable called media. You had defined a private function called _location it was fine, but when you were using this function you are passing an undefined and un-inited variable called media.
try
late String myUrl;
void _location(dynamic media){
myUrl = media['url'];
}
_location(media);

get data from constructor and pass it to variable

I want to get the data from the constructor and passes it to a variable.
I dont want it to be listed...
Class:
class ListaProjetos extends StatefulWidget {
ListaProjetos({Key key, this.title, this.jsonData}) : super(key: key);
static const String routeName = "/ListaProjetos";
final String title;
final List jsonData;
#override
_ListaProjetosState createState() => _ListaProjetosState();
}
I want to:
class _ListaProjetosState extends State<ListaProjetos> {
var message = widget.jsonData;
print(message);
//HERE I WANT TO PRINT ALL JSON DATA
}
Inside the class you need create a method and place the variables initialization, for example:
#override
void initState() {
var message = widget.jsonData;
print(message);
}
Or you can declare the var outside of the method to use it in others methods.
var message;
#override
void initState() {
message = widget.jsonData;
print(message);
}

The instance 'widget' can't be accessed in an initializer

I'm having trouble accessing the variable user on _MenuScreenState:
class MenuScreen extends StatefulWidget {
final User user;
MenuScreen(this.user);
#override
_MenuScreenState createState() => _MenuScreenState();
}
class _MenuScreenState extends State<MenuScreen> {
final User userInMenu = widget.user;
}
The problem displayed is "The instance member 'widget' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression".
You have to do it inside initState like the following:
class MenuScreen extends StatefulWidget {
final User user;
MenuScreen(this.user);
#override
_MenuScreenState createState() => _MenuScreenState();
}
class _MenuScreenState extends State<MenuScreen> {
User userInMenu = widget.user;
#override
void initState() {
super.initState();
userInMenu = widget.user;
}

How can I use variable of CubitState inside Cubit? Flutter/Bloc

so I don't have any idea how to take argument from mine Cubit state which is AnswerPicked in this case, there is a code from states file.
part of 'answer_cubit.dart';
abstract class AnswerState extends Equatable {
const AnswerState();
#override
List<Object> get props => [];
}
class AnswerInitial extends AnswerState {}
class AnswerPicked extends AnswerState {
final String answer;
AnswerPicked({
this.answer,
});
String toString() => '{AnswerPicked: $answer}';
}
I want to use it in Cubit function right there:
part 'answer_state.dart';
class AnswerCubit extends Cubit<AnswerState> {
final ExamScoreCubit scoreCubit;
AnswerCubit({
#required this.scoreCubit,
}) : super(AnswerInitial());
List<String> userAnswersList = [];
void pickAnswer(String answer) {
emit(AnswerInitial());
emit(AnswerPicked(answer: answer));
}
void takeAnswer(String questionAnswer, int type) {
if(state is AnswerPicked){
userAnswersList.add(state.answer); // state.answer don't work
scoreCubit.checkAnswer(AnswerPicked().answer, questionAnswer, type); // AnswerPicked().answer don't work
}
emit(AnswerInitial());
}
}
In void takeAnswer() I don't want to pass it throw argument inside the widget tree using context. Any ideas how to do it?
userAnswersList.add((state as AnswerPicked) .answer);

flutter - clear the widget cache

When I leave a page when I work with flutter and then re-enter it, the old values are saved.
I don't want this to happen.
For example, when I first enter:
int number = 1;
class TestPage extends StatefulWidget {
#override
_TestPageState createState() => _TestPageState();
}
bool donus = true;
class _TestPageState extends State<TestPage> {
List<Todo> sorular = new List<Todo>();
#override
void initState() {
print(donus);
super.initState();
getTodos();
}
bool pressAttention = true;
String ileri = "İleri", geri = "Geri";
#override
Widget build(BuildContext context) {number++;
print(number);
output:1
Second entry to the page:
output:2
I do not know how to fix this.
The scope of your number variable is outside the scope of your page and its state.
number is essentially static or global, and its lifetime is tied to your application's. Tighten up its scope so that it is declared within your state class and initialise it in your initState method (for private non-final variables).
class TestPage extends StatefulWidget {
#override
_TestPageState createState() => _TestPageState();
}
bool donus = true;
class _TestPageState extends State<TestPage> {
List<Todo> sorular = new List<Todo>();
int _number;
#override
void initState() {
_number = 1;
print(donus);
super.initState();
getTodos();
}
bool pressAttention = true;
String ileri = "İleri", geri = "Geri";
#override
Widget build(BuildContext context) {
_number++; // You probably don't want to increment every time you build,
// but I have no idea what you're using `number` for.
print(_number);
Your donus variable is also global, which is unlikely to be what you had intended. Dart is an object-oriented language; refer to the language tour for more details on classes, scope, etc.