I face this problem when I want to add lists.dart in BottomNavigationBarItem. in bottom of scaffold it shows red underline- body: _children[_currentIndex],
but don't know, what is the actual problem. what does it mean by
The operator '[]' isn't defined for the type 'List'
?
this is the full code of main.dart
void main() {
runApp(Main());
}
class Main extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.deepPurple,
),
home: MainPage(),
);
}
}
class MainPage extends StatefulWidget {
const MainPage({Key? key}) : super(key: key);
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
late SharedPreferences sharedPreferences;
int _currentIndex = 0;
final List _children = [
Home(),
OrganizationData(),
Notifications(),
More(),
] as List;
void onTapped(int index) {
setState(() {
_currentIndex = index;
});
}
@override
void initState() {
super.initState();
checkLoginStatus();
}
checkLoginStatus() async {
sharedPreferences = await SharedPreferences.getInstance();
if (sharedPreferences.getString("token") == null) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (BuildContext context) => LoginPage()),
(Route<dynamic> route) => false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leadingWidth: 80,
toolbarHeight: 80,
titleSpacing: -15.0,
title: ListTile(
title: Text(
"FAYZULLAH",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
subtitle: Text(
"Jr. executive",
style: TextStyle(
fontSize: 12,
),
),
),
leading: GestureDetector(
onTap: () {},
child: Container(
padding: EdgeInsets.all(10.0),
margin: EdgeInsets.all(10.0),
decoration: new BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(50.0),
),
border: Border.all(
width: 1,
style: BorderStyle.solid,
color: Colors.black12,
),
image: DecorationImage(
image: AssetImage('assets/fozuda.png'),
),
),
),
),
actions: [
IconButton(
icon: Icon(
Icons.logout_rounded,
size: 20,
),
color: Colors.black,
onPressed: () {
sharedPreferences.clear();
sharedPreferences.commit();
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (BuildContext context) => LoginPage()),
(Route<dynamic> route) => false);
},
),
],
backgroundColor: Colors.yellow,
),
bottomNavigationBar: BottomNavigationBar(
onTap: onTapped,
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
// backgroundColor: Colors.yellow,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.list),
label: 'Lists',
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications),
label: 'Notification',
),
BottomNavigationBarItem(
icon: Icon(Icons.more_horiz_rounded),
label: 'More',
),
],
),
body: _children[_currentIndex],
);
}
}
and this is the lists.dart code
class OrganizationData extends StatefulWidget {
const OrganizationData({Key? key}) : super(key: key);
@override
_OrganizationDataState createState() => _OrganizationDataState();
}
class _OrganizationDataState extends State<OrganizationData> {
late Future<List> futureAlbum;
@override
void initState() {
super.initState();
futureAlbum = listData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder<List>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
// print(snapshot.data);
return Text(snapshot.data!.fullName);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
),
),
);
}
}
Future<List> listData() async {
Dio dio = new Dio();
dio.options.headers['Content-Type'] = 'application/json';
final body = {'limit': 1, 'orderBy': 'idEmployee', 'orderType': 'DESC'};
final response = await dio.post(url, data: body);
if (response.statusCode == 200) {
return List.fromJson(response.data["data"]["data"][0]);
} else {
throw Exception('Failed!');
}
}
class List {
final int idEmployee;
final String fullName;
List({
required this.idEmployee,
required this.fullName,
});
factory List.fromJson(Map<String, dynamic> json) {
return List(
idEmployee: json['idEmployee'],
fullName: json['fullName'],
);
}
}
Rename your List
class in lists.dart
in some other name and correct the name everywhere you used your List
class.
Because there is a built in object named List
So, you cannot name your classes "List".
Naming your own classes "List" will override the built in List
class. That's why dart tells that "The operator '[]' isn't defined for the type 'List'".
So, change your class name everywhere you used the class.