I got this error from the terminal:
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 got this error because I'm doing a YouTube tutorial and the YouTuber have:
child: TaskWidget(
text: controller.myData[index]["task_name"],
But in his code doesn't have any error and can run the data with no problems, I have an error when I type “controller”
This is my code: all_tasks.dart
import 'package:flutter/material.dart';
import 'package:flutter_golang_yt/colors/app_colors.dart';
import 'package:flutter_golang_yt/controllers/data_controller.dart';
import 'package:flutter_golang_yt/widgets/button_widget.dart';
import 'package:flutter_golang_yt/widgets/task_widget.dart';
import 'package:get/get.dart';
class AllTasks extends StatelessWidget {
const AllTasks({Key? key}) : super(key: key);
_loadData() async {
await Get.find<DataController>().getData();
}
@override
Widget build(BuildContext context) {
print(Get.find<DataController>().myData.length);
_loadData();
List myData =[
" todo 1",
" todo 2"
];
final leftEditIcon = Container(
margin: const EdgeInsets.only(bottom: 10),
color:const Color(0xFF2e3253),
child: const Icon(
Icons.edit,
color: Colors.white,
),
alignment: Alignment.centerLeft,
);
final rightDeleteIcon = Container(
margin: const EdgeInsets.only(bottom: 10),
color:Colors.redAccent,
child: const Icon(
Icons.delete,
color: Colors.white,
),
alignment: Alignment.centerRight,
);
return Scaffold(
backgroundColor: Colors.white,
body: Column(
children: [
Container(
padding: const EdgeInsets.only(left: 20, top: 60),
alignment: Alignment.topLeft,
child: InkWell(
onTap: (){
Get.back();
},
child: Icon(Icons.arrow_back, color: AppColors.secondaryColor,),
),
width: double.maxFinite,
height: MediaQuery.of(context).size.height/3.2,
decoration: const BoxDecoration(
image: DecorationImage(
fit:BoxFit.cover,
image: AssetImage(
"assets/header1.jpg"
)
)
),
),
Container(
padding: const EdgeInsets.only(left: 20, right: 20),
child: Row(
children: [
Icon(Icons.home, color: AppColors.secondaryColor,),
SizedBox(width: 10,),
Container(
child: Icon(Icons.add, color: Colors.white, size: 20,),
width: 25,
height: 25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.5),
color: Colors.black
),
),
Expanded(child: Container()),
Icon(Icons.calendar_month_sharp, color: AppColors.secondaryColor,),
SizedBox(width: 10,),
Text("2", style: TextStyle(
fontSize: 20,
color: AppColors.secondaryColor
),)
],
),
),
Flexible(
child: ListView.builder(
itemCount: myData.length,
itemBuilder: (context, index){
return Dismissible(
background: leftEditIcon,
secondaryBackground: rightDeleteIcon,
onDismissed: (DismissDirection direction){
print("after dismiss");
},
confirmDismiss: (DismissDirection direction)async{
if(direction==DismissDirection.startToEnd){
showModalBottomSheet(
backgroundColor: Colors.transparent,
barrierColor: Colors.transparent,
context: context,
builder: (_){
return Container(
height: 550,
decoration: BoxDecoration(
color: const Color(0xFF2e3253).withOpacity(0.4),
borderRadius: const BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20),
)
),
child: Padding(
padding: const EdgeInsets.only(left: 20, right: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ButtonWidget(backgroundcolor: AppColors.mainColor, text: "View", textColor: Colors.white),
SizedBox(height: 20,),
ButtonWidget(backgroundcolor: AppColors.mainColor, text: "Edit", textColor: Colors.white),
],
),
),
);
});
return false;
}else{
return Future.delayed(Duration(seconds: 1),()=>direction==DismissDirection.endToStart);
}
},
key: ObjectKey(index),
child: Container(
margin: const EdgeInsets.only(
left: 20,
right: 20,
bottom: 10),
child: TaskWidget(
text: controller.myData[index]["task_name"],
color: Colors.blueGrey,
),
),
);
}),
)
],
),
);
}
}
What should I do?
CodePudding user response:
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.
CodePudding user response:
You haven't declared controller.
try this.
class AllTasks extends StatefullWidget {
late final controller;
@override
void initState() {
controller = Get.find<DataController>();
super.initState();
}