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 ??
CodePudding user response:
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.
CodePudding user response:
try
late String myUrl;
void _location(dynamic media){
myUrl = media['url'];
}
_location(media);