When I am accessing the locations list that I just get from the fetchAll() function. I cannot use it.I am just starting with flutter so excuse my silly mistakes.
I was following the tutorial and then I got this error. I read answers to a similar questions but was not able to fix them. Hope you guys can suggest some solutions. Thanks in advance for your help.
code for model location class
import 'location_fact.dart';
class Location {
String name;
String imagePath;
final List<LocationFact> facts;
Location(this.name, this.imagePath, this.facts);
static List<Location> fetchAll() {
return [
Location("Aarashiyama Mountains", "assets/images/sceneryfd.jpg", [
LocationFact('Summary',
'While we could go on about the ethereal glow and seemingly endless heights of this bamboo grove on the outskirts of Kyoto, the sight\'s pleasures extend beyond the visual realm.'),
LocationFact('How to Get There',
'Kyoto airport, with several terminals, is located 16 kilometres south of the city and is also known as Kyoto. Kyoto can also be reached by transport links from other regional airports.'),
])
];
}
}
code for location_details class
import 'dart:html';
import 'package:flutter/material.dart';
import 'package:applications/screens/location_details/textSection.dart';
import '../../style.dart';
import 'package:applications/models/location.dart';
// some relative comments.
// stless for stateless snippet.
void main() {
runApp(const LocationDetail());
}
class LocationDetail extends StatelessWidget {
const LocationDetail({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LocationMain(),
theme: ThemeData(
textTheme: const TextTheme(
titleMedium: TitleTextStyle,
bodyMedium: BodyTextStyle
)
),
);
}
}
class LocationMain extends StatelessWidget {
final locations = Location.fetchAll();
final location = location.first;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(location.name),
titleTextStyle: AppBarTextStyle,
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ImageBanner(location.imagePath),
TextSection("Title 1", "a;dfka;fjakdjf"),
TextSection("Title 2", "This is Lorem Ipsum, but Indian version. Tussi kya dekhna chahte ho? :)"),
],
));
}
}
class ImageBanner extends StatelessWidget {
final String _path;
const ImageBanner(this._path);
@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints.expand(
height: 200.0,
),
child: Image.asset(
_path,
fit: BoxFit.cover,
),
);
}
}
error text
lib/screens/location_details/location_details.dart:38:20: Error: Can't access 'this' in a field initializer to read 'location'. final location = location.first; ^^^^^^^^