简体   繁体   中英

The method 'getContacts' isn't defined for the type 'Type'

I'm new to the GetX framework and am trying the call the controller class function from the view using the Elevated button but it causes an error.

The method 'getContacts' isn't defined for the type 'Type'.

this is my contoller 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");
    }
  }
}

this is my view 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")));
  }
}

I can't able to call the function inside the elevated Button

onPressed:()=>contactController.getContacts(),

any one help me to solve this issue

Try changing it

final contactController = Get.put(ContactController());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM