I'm encountering the following error in my Flutter application:
lib/screens/all_tasks.dart:141:27: Error: The getter 'controller' isn't defined for the class 'AllTasks'.
- 'AllTasks' is from 'package:flutter_golang_yt/screens/all_tasks.dart' ('lib/screens/all_tasks.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'controller'.
text: controller.myData[index]["task_name"],
^^^^^^^^^^
I understand that the error indicates the controller
variable is not defined within the AllTasks
class. I'm following a tutorial where the instructor uses controller
without issue, and I'm unsure why it's failing for me.
Here's the relevant portion of my all_tasks.dart
file:
import 'package:flutter/material.dart';
import 'package:flutter_golang_yt/controllers/data_controller.dart';
// ... other imports
class AllTasks extends StatelessWidget {
const AllTasks({Key? key}) : super(key: key);
// ... other methods
@override
Widget build(BuildContext context) {
// ... other code
return Scaffold(
// ... other code
body: Column(
children: [
// ... other widgets
Flexible(
child: ListView.builder(
itemCount: myData.length,
itemBuilder: (context, index) {
return Dismissible(
// ... other code
child: Container(
margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10),
child: TaskWidget(
text: controller.myData[index]["task_name"], // <-- Problem line
color: Colors.blueGrey,
),
),
);
},
),
)
],
),
);
}
}
I suspect I might be missing a setup step or a dependency. Can anyone help me understand how to properly use and access the controller
within my AllTasks
class?
If you are using GetxBindings, you can make your widget inherit from GetView
, as the example below:
class AllTasks extends GetView<DataController> {
...
If you don't, you should store the response from Get.find<DataController>()
in a variable called controller
.
class AllTasks extends StatelessWidget {
late final DataController controller;
const AllTasks({Key? key}) : super(key: key);
@override
void initState() {
controller = Get.find<DataController>();
super.initState();
}
...
So, you can use controller
in your widget.