方法 'getContacts' 没有为类型 'Type' 定义
The method 'getContacts' isn't defined for the type 'Type'
我是 GetX 框架的新手,正在尝试使用 Elevated 按钮从视图调用控制器 class 函数,但它会导致错误。
The method 'getContacts' isn't defined for the type 'Type'.
这是我的控制器class
class ContactController extends GetxController {
var id;
@override
void onInit() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
id = preferences.getString('id');
print(id);
}
getContact() async {
// SERVER API URL
Uri url = Uri.parse('http://localhost:8000/get-contacts');
// Store all data with Param Name.
var data = {'sender': id};
//json encode
String? body = json.encode(data);
// Starting Web API Call.
var response = await http.post(url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: body);
// Getting Server response into variable.
var contact = jsonDecode(response.body);
// If Web call Success than Hide the CircularProgressIndicator.
if (response.statusCode == 200) {
print(contact);
} else {
print("error");
}
}
}
这是我的看法class
class Contacts extends StatefulWidget {
const Contacts({Key? key}) : super(key: key);
@override
State<Contacts> createState() => _ContactsState();
}
class _ContactsState extends State<Contacts> {
final contactController = Get.put((ContactController));
@override
Widget build(BuildContext context) {
return Container(
child: ElevatedButton(
onPressed:()=>contactController.getContacts(),
child: Text("GET CONTACT")));
}
}
我无法调用提升按钮内的函数
onPressed:()=>contactController.getContacts(),
谁能帮我解决这个问题
尝试更改它
final contactController = Get.put(ContactController());
我是 GetX 框架的新手,正在尝试使用 Elevated 按钮从视图调用控制器 class 函数,但它会导致错误。
The method 'getContacts' isn't defined for the type 'Type'.
这是我的控制器class
class ContactController extends GetxController {
var id;
@override
void onInit() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
id = preferences.getString('id');
print(id);
}
getContact() async {
// SERVER API URL
Uri url = Uri.parse('http://localhost:8000/get-contacts');
// Store all data with Param Name.
var data = {'sender': id};
//json encode
String? body = json.encode(data);
// Starting Web API Call.
var response = await http.post(url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: body);
// Getting Server response into variable.
var contact = jsonDecode(response.body);
// If Web call Success than Hide the CircularProgressIndicator.
if (response.statusCode == 200) {
print(contact);
} else {
print("error");
}
}
}
这是我的看法class
class Contacts extends StatefulWidget {
const Contacts({Key? key}) : super(key: key);
@override
State<Contacts> createState() => _ContactsState();
}
class _ContactsState extends State<Contacts> {
final contactController = Get.put((ContactController));
@override
Widget build(BuildContext context) {
return Container(
child: ElevatedButton(
onPressed:()=>contactController.getContacts(),
child: Text("GET CONTACT")));
}
}
我无法调用提升按钮内的函数
onPressed:()=>contactController.getContacts(),
谁能帮我解决这个问题
尝试更改它
final contactController = Get.put(ContactController());