I want to be able to have the value of a variable that I receive as a parameter from another screen through the class and insert the value of the variable in the elements of an array: import 'package:flutter/material.dart'; import 'package:carousel_slider/carousel_slider.dart';
class FotosPage extends StatefulWidget {
final String id; //Este es el valor que recibo desde screen anterior
FotosPage(this.id,{Key key}): super(key: key);
@override
FotosPageState createState() => FotosPageState();
}
class FotosPageState extends State<FotosPage> {
CarouselSlider carouselSlider;
int _current = 0;
String _num;
//Aqui trato de usar la variable _num y me arroja error: only static members can be accessed in initializers
List imgList = [
'https://xxxxxxxxxx.com/read/${_num}/1.jpg',
'https://xxxxxxxxxx.com/read/${_num}/2.jpg',
'https://xxxxxxxxxx.com/read/${_num}/3.jpg',
];
@override
initState() {
_num = widget.id; //Paso el valor del parámetro recibido a la variable
super.initState();
}
}
The error is clear, you cannot access values
initializers
that are declared as variables, using non-static attributes.To solve it you can do the following: